<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<meta name="generator" content="AsciiDoc 8.6.9" />
<title>Rosetta Code</title>
<style type="text/css">
/* Shared CSS for AsciiDoc xhtml11 and html5 backends */

/* Default font. */
body {
  font-family: Georgia,serif;
}

/* Title font. */
h1, h2, h3, h4, h5, h6,
div.title, caption.title,
thead, p.table.header,
#toctitle,
#author, #revnumber, #revdate, #revremark,
#footer {
  font-family: Arial,Helvetica,sans-serif;
}

body {
  margin: 1em 5% 1em 5%;
}

a {
  color: blue;
  text-decoration: underline;
}
a:visited {
  color: fuchsia;
}

em {
  font-style: italic;
  color: navy;
}

strong {
  font-weight: bold;
  color: #083194;
}

h1, h2, h3, h4, h5, h6 {
  color: #527bbd;
  margin-top: 1.2em;
  margin-bottom: 0.5em;
  line-height: 1.3;
}

h1, h2, h3 {
  border-bottom: 2px solid silver;
}
h2 {
  padding-top: 0.5em;
}
h3 {
  float: left;
}
h3 + * {
  clear: left;
}
h5 {
  font-size: 1.0em;
}

div.sectionbody {
  margin-left: 0;
}

hr {
  border: 1px solid silver;
}

p {
  margin-top: 0.5em;
  margin-bottom: 0.5em;
}

ul, ol, li > p {
  margin-top: 0;
}
ul > li     { color: #aaa; }
ul > li > * { color: black; }

.monospaced, code, pre {
  font-family: "Courier New", Courier, monospace;
  font-size: inherit;
  color: navy;
  padding: 0;
  margin: 0;
}
pre {
  white-space: pre-wrap;
}

#author {
  color: #527bbd;
  font-weight: bold;
  font-size: 1.1em;
}
#email {
}
#revnumber, #revdate, #revremark {
}

#footer {
  font-size: small;
  border-top: 2px solid silver;
  padding-top: 0.5em;
  margin-top: 4.0em;
}
#footer-text {
  float: left;
  padding-bottom: 0.5em;
}
#footer-badges {
  float: right;
  padding-bottom: 0.5em;
}

#preamble {
  margin-top: 1.5em;
  margin-bottom: 1.5em;
}
div.imageblock, div.exampleblock, div.verseblock,
div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
div.admonitionblock {
  margin-top: 1.0em;
  margin-bottom: 1.5em;
}
div.admonitionblock {
  margin-top: 2.0em;
  margin-bottom: 2.0em;
  margin-right: 10%;
  color: #606060;
}

div.content { /* Block element content. */
  padding: 0;
}

/* Block element titles. */
div.title, caption.title {
  color: #527bbd;
  font-weight: bold;
  text-align: left;
  margin-top: 1.0em;
  margin-bottom: 0.5em;
}
div.title + * {
  margin-top: 0;
}

td div.title:first-child {
  margin-top: 0.0em;
}
div.content div.title:first-child {
  margin-top: 0.0em;
}
div.content + div.title {
  margin-top: 0.0em;
}

div.sidebarblock > div.content {
  background: #ffffee;
  border: 1px solid #dddddd;
  border-left: 4px solid #f0f0f0;
  padding: 0.5em;
}

div.listingblock > div.content {
  border: 1px solid #dddddd;
  border-left: 5px solid #f0f0f0;
  background: #f8f8f8;
  padding: 0.5em;
}

div.quoteblock, div.verseblock {
  padding-left: 1.0em;
  margin-left: 1.0em;
  margin-right: 10%;
  border-left: 5px solid #f0f0f0;
  color: #888;
}

div.quoteblock > div.attribution {
  padding-top: 0.5em;
  text-align: right;
}

div.verseblock > pre.content {
  font-family: inherit;
  font-size: inherit;
}
div.verseblock > div.attribution {
  padding-top: 0.75em;
  text-align: left;
}
/* DEPRECATED: Pre version 8.2.7 verse style literal block. */
div.verseblock + div.attribution {
  text-align: left;
}

div.admonitionblock .icon {
  vertical-align: top;
  font-size: 1.1em;
  font-weight: bold;
  text-decoration: underline;
  color: #527bbd;
  padding-right: 0.5em;
}
div.admonitionblock td.content {
  padding-left: 0.5em;
  border-left: 3px solid #dddddd;
}

div.exampleblock > div.content {
  border-left: 3px solid #dddddd;
  padding-left: 0.5em;
}

div.imageblock div.content { padding-left: 0; }
span.image img { border-style: none; vertical-align: text-bottom; }
a.image:visited { color: white; }

dl {
  margin-top: 0.8em;
  margin-bottom: 0.8em;
}
dt {
  margin-top: 0.5em;
  margin-bottom: 0;
  font-style: normal;
  color: navy;
}
dd > *:first-child {
  margin-top: 0.1em;
}

ul, ol {
    list-style-position: outside;
}
ol.arabic {
  list-style-type: decimal;
}
ol.loweralpha {
  list-style-type: lower-alpha;
}
ol.upperalpha {
  list-style-type: upper-alpha;
}
ol.lowerroman {
  list-style-type: lower-roman;
}
ol.upperroman {
  list-style-type: upper-roman;
}

div.compact ul, div.compact ol,
div.compact p, div.compact p,
div.compact div, div.compact div {
  margin-top: 0.1em;
  margin-bottom: 0.1em;
}

tfoot {
  font-weight: bold;
}
td > div.verse {
  white-space: pre;
}

div.hdlist {
  margin-top: 0.8em;
  margin-bottom: 0.8em;
}
div.hdlist tr {
  padding-bottom: 15px;
}
dt.hdlist1.strong, td.hdlist1.strong {
  font-weight: bold;
}
td.hdlist1 {
  vertical-align: top;
  font-style: normal;
  padding-right: 0.8em;
  color: navy;
}
td.hdlist2 {
  vertical-align: top;
}
div.hdlist.compact tr {
  margin: 0;
  padding-bottom: 0;
}

.comment {
  background: yellow;
}

.footnote, .footnoteref {
  font-size: 0.8em;
}

span.footnote, span.footnoteref {
  vertical-align: super;
}

#footnotes {
  margin: 20px 0 20px 0;
  padding: 7px 0 0 0;
}

#footnotes div.footnote {
  margin: 0 0 5px 0;
}

#footnotes hr {
  border: none;
  border-top: 1px solid silver;
  height: 1px;
  text-align: left;
  margin-left: 0;
  width: 20%;
  min-width: 100px;
}

div.colist td {
  padding-right: 0.5em;
  padding-bottom: 0.3em;
  vertical-align: top;
}
div.colist td img {
  margin-top: 0.3em;
}

@media print {
  #footer-badges { display: none; }
}

#toc {
  margin-bottom: 2.5em;
}

#toctitle {
  color: #527bbd;
  font-size: 1.1em;
  font-weight: bold;
  margin-top: 1.0em;
  margin-bottom: 0.1em;
}

div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
  margin-top: 0;
  margin-bottom: 0;
}
div.toclevel2 {
  margin-left: 2em;
  font-size: 0.9em;
}
div.toclevel3 {
  margin-left: 4em;
  font-size: 0.9em;
}
div.toclevel4 {
  margin-left: 6em;
  font-size: 0.9em;
}

span.aqua { color: aqua; }
span.black { color: black; }
span.blue { color: blue; }
span.fuchsia { color: fuchsia; }
span.gray { color: gray; }
span.green { color: green; }
span.lime { color: lime; }
span.maroon { color: maroon; }
span.navy { color: navy; }
span.olive { color: olive; }
span.purple { color: purple; }
span.red { color: red; }
span.silver { color: silver; }
span.teal { color: teal; }
span.white { color: white; }
span.yellow { color: yellow; }

span.aqua-background { background: aqua; }
span.black-background { background: black; }
span.blue-background { background: blue; }
span.fuchsia-background { background: fuchsia; }
span.gray-background { background: gray; }
span.green-background { background: green; }
span.lime-background { background: lime; }
span.maroon-background { background: maroon; }
span.navy-background { background: navy; }
span.olive-background { background: olive; }
span.purple-background { background: purple; }
span.red-background { background: red; }
span.silver-background { background: silver; }
span.teal-background { background: teal; }
span.white-background { background: white; }
span.yellow-background { background: yellow; }

span.big { font-size: 2em; }
span.small { font-size: 0.6em; }

span.underline { text-decoration: underline; }
span.overline { text-decoration: overline; }
span.line-through { text-decoration: line-through; }

div.unbreakable { page-break-inside: avoid; }


/*
 * xhtml11 specific
 *
 * */

div.tableblock {
  margin-top: 1.0em;
  margin-bottom: 1.5em;
}
div.tableblock > table {
  border: 3px solid #527bbd;
}
thead, p.table.header {
  font-weight: bold;
  color: #527bbd;
}
p.table {
  margin-top: 0;
}
/* Because the table frame attribute is overriden by CSS in most browsers. */
div.tableblock > table[frame="void"] {
  border-style: none;
}
div.tableblock > table[frame="hsides"] {
  border-left-style: none;
  border-right-style: none;
}
div.tableblock > table[frame="vsides"] {
  border-top-style: none;
  border-bottom-style: none;
}


/*
 * html5 specific
 *
 * */

table.tableblock {
  margin-top: 1.0em;
  margin-bottom: 1.5em;
}
thead, p.tableblock.header {
  font-weight: bold;
  color: #527bbd;
}
p.tableblock {
  margin-top: 0;
}
table.tableblock {
  border-width: 3px;
  border-spacing: 0px;
  border-style: solid;
  border-color: #527bbd;
  border-collapse: collapse;
}
th.tableblock, td.tableblock {
  border-width: 1px;
  padding: 4px;
  border-style: solid;
  border-color: #527bbd;
}

table.tableblock.frame-topbot {
  border-left-style: hidden;
  border-right-style: hidden;
}
table.tableblock.frame-sides {
  border-top-style: hidden;
  border-bottom-style: hidden;
}
table.tableblock.frame-none {
  border-style: hidden;
}

th.tableblock.halign-left, td.tableblock.halign-left {
  text-align: left;
}
th.tableblock.halign-center, td.tableblock.halign-center {
  text-align: center;
}
th.tableblock.halign-right, td.tableblock.halign-right {
  text-align: right;
}

th.tableblock.valign-top, td.tableblock.valign-top {
  vertical-align: top;
}
th.tableblock.valign-middle, td.tableblock.valign-middle {
  vertical-align: middle;
}
th.tableblock.valign-bottom, td.tableblock.valign-bottom {
  vertical-align: bottom;
}


/*
 * manpage specific
 *
 * */

body.manpage h1 {
  padding-top: 0.5em;
  padding-bottom: 0.5em;
  border-top: 2px solid silver;
  border-bottom: 2px solid silver;
}
body.manpage h2 {
  border-style: none;
}
body.manpage div.sectionbody {
  margin-left: 3em;
}

@media print {
  body.manpage div#toc { display: none; }
}


</style>
<script type="text/javascript">
/*<![CDATA[*/
var asciidoc = {  // Namespace.

/////////////////////////////////////////////////////////////////////
// Table Of Contents generator
/////////////////////////////////////////////////////////////////////

/* Author: Mihai Bazon, September 2002
 * http://students.infoiasi.ro/~mishoo
 *
 * Table Of Content generator
 * Version: 0.4
 *
 * Feel free to use this script under the terms of the GNU General Public
 * License, as long as you do not remove or alter this notice.
 */

 /* modified by Troy D. Hanson, September 2006. License: GPL */
 /* modified by Stuart Rackham, 2006, 2009. License: GPL */

// toclevels = 1..4.
toc: function (toclevels) {

  function getText(el) {
    var text = "";
    for (var i = el.firstChild; i != null; i = i.nextSibling) {
      if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
        text += i.data;
      else if (i.firstChild != null)
        text += getText(i);
    }
    return text;
  }

  function TocEntry(el, text, toclevel) {
    this.element = el;
    this.text = text;
    this.toclevel = toclevel;
  }

  function tocEntries(el, toclevels) {
    var result = new Array;
    var re = new RegExp('[hH]([1-'+(toclevels+1)+'])');
    // Function that scans the DOM tree for header elements (the DOM2
    // nodeIterator API would be a better technique but not supported by all
    // browsers).
    var iterate = function (el) {
      for (var i = el.firstChild; i != null; i = i.nextSibling) {
        if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
          var mo = re.exec(i.tagName);
          if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
            result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
          }
          iterate(i);
        }
      }
    }
    iterate(el);
    return result;
  }

  var toc = document.getElementById("toc");
  if (!toc) {
    return;
  }

  // Delete existing TOC entries in case we're reloading the TOC.
  var tocEntriesToRemove = [];
  var i;
  for (i = 0; i < toc.childNodes.length; i++) {
    var entry = toc.childNodes[i];
    if (entry.nodeName.toLowerCase() == 'div'
     && entry.getAttribute("class")
     && entry.getAttribute("class").match(/^toclevel/))
      tocEntriesToRemove.push(entry);
  }
  for (i = 0; i < tocEntriesToRemove.length; i++) {
    toc.removeChild(tocEntriesToRemove[i]);
  }

  // Rebuild TOC entries.
  var entries = tocEntries(document.getElementById("content"), toclevels);
  for (var i = 0; i < entries.length; ++i) {
    var entry = entries[i];
    if (entry.element.id == "")
      entry.element.id = "_toc_" + i;
    var a = document.createElement("a");
    a.href = "#" + entry.element.id;
    a.appendChild(document.createTextNode(entry.text));
    var div = document.createElement("div");
    div.appendChild(a);
    div.className = "toclevel" + entry.toclevel;
    toc.appendChild(div);
  }
  if (entries.length == 0)
    toc.parentNode.removeChild(toc);
},


/////////////////////////////////////////////////////////////////////
// Footnotes generator
/////////////////////////////////////////////////////////////////////

/* Based on footnote generation code from:
 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
 */

footnotes: function () {
  // Delete existing footnote entries in case we're reloading the footnodes.
  var i;
  var noteholder = document.getElementById("footnotes");
  if (!noteholder) {
    return;
  }
  var entriesToRemove = [];
  for (i = 0; i < noteholder.childNodes.length; i++) {
    var entry = noteholder.childNodes[i];
    if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote")
      entriesToRemove.push(entry);
  }
  for (i = 0; i < entriesToRemove.length; i++) {
    noteholder.removeChild(entriesToRemove[i]);
  }

  // Rebuild footnote entries.
  var cont = document.getElementById("content");
  var spans = cont.getElementsByTagName("span");
  var refs = {};
  var n = 0;
  for (i=0; i<spans.length; i++) {
    if (spans[i].className == "footnote") {
      n++;
      var note = spans[i].getAttribute("data-note");
      if (!note) {
        // Use [\s\S] in place of . so multi-line matches work.
        // Because JavaScript has no s (dotall) regex flag.
        note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
        spans[i].innerHTML =
          "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
          "' title='View footnote' class='footnote'>" + n + "</a>]";
        spans[i].setAttribute("data-note", note);
      }
      noteholder.innerHTML +=
        "<div class='footnote' id='_footnote_" + n + "'>" +
        "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
        n + "</a>. " + note + "</div>";
      var id =spans[i].getAttribute("id");
      if (id != null) refs["#"+id] = n;
    }
  }
  if (n == 0)
    noteholder.parentNode.removeChild(noteholder);
  else {
    // Process footnoterefs.
    for (i=0; i<spans.length; i++) {
      if (spans[i].className == "footnoteref") {
        var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
        href = href.match(/#.*/)[0];  // Because IE return full URL.
        n = refs[href];
        spans[i].innerHTML =
          "[<a href='#_footnote_" + n +
          "' title='View footnote' class='footnote'>" + n + "</a>]";
      }
    }
  }
},

install: function(toclevels) {
  var timerId;

  function reinstall() {
    asciidoc.footnotes();
    if (toclevels) {
      asciidoc.toc(toclevels);
    }
  }

  function reinstallAndRemoveTimer() {
    clearInterval(timerId);
    reinstall();
  }

  timerId = setInterval(reinstall, 500);
  if (document.addEventListener)
    document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false);
  else
    window.onload = reinstallAndRemoveTimer;
}

}
asciidoc.install(2);
/*]]>*/
</script>
</head>
<body class="article">
<div id="header">
<h1>Rosetta Code</h1>
<div id="toc">
  <div id="toctitle">Table of Contents</div>
  <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<style>
h2 {
        font-size: 14pt;
}

pre {
        font-family: Fantasque Sans Mono;
        font-weight: bold;
        font-size: 8pt;
}
</style>
</div>
</div>
<div class="sect1">
<h2 id="_code_data_structure_bit_rmq_cc_code">1. <code>data_structure/bit-rmq.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// 题意：给定一个数列，支持单调修改，询问，x0, x0+d, x0+2d, x0+3d...这个等差数列的最大值。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：当d很大时，暴力效率比较高，但是d很小时暴力效率低，所以考虑分块。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       对d &gt; k(d &lt;= k)分块。小的时候，建立k系列个关于数列的树状数组，对于</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       第i个系列，讲整个数列关于%i划分，这样就可以建立%i个树状数组来连续</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       维护原来的等差数列。树状数组做rmq。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       复杂度是O(m*( (n/k + log(n/d)) + (k*log^2(n/d)) )), 前一半是查询，后一半是修改。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       这样可得理论最优分块大小为 k = sqrt(n) / log(n)。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>

<span style="font-style: italic"><span style="color: #9A1900">// run: time $exec &lt; input &gt; output</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// opt: 0</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// flag: -g</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cmath&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">70007</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> da<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">,</span> piece<span style="color: #990000">;</span>

std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;std::vector&lt;int&gt; &gt; &gt;</span> bit<span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">lowbit</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> x <span style="color: #990000">&amp;</span> <span style="color: #990000">(-</span>x<span style="color: #990000">);</span> <span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        bit<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>piece <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> piece<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                bit<span style="color: #990000">[</span>i<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>i<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> i<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> bit<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> p <span style="color: #990000">=</span> j <span style="color: #990000">%</span> i<span style="color: #990000">;</span>
                        bit<span style="color: #990000">[</span>i<span style="color: #990000">][</span>p<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>da<span style="color: #990000">[</span>j<span style="color: #990000">]);</span>
                        bit<span style="color: #990000">[</span>i<span style="color: #990000">][</span>p<span style="color: #990000">][</span><span style="color: #993399">0</span><span style="color: #990000">]++;</span>
                <span style="color: #FF0000">}</span>

                <span style="font-style: italic"><span style="color: #9A1900">// init every remainder a bit</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> i<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> k <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> k <span style="color: #990000">&lt;=</span> bit<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">][</span><span style="color: #993399">0</span><span style="color: #990000">];</span> k<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> t <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> t <span style="color: #990000">&lt;</span> <span style="font-weight: bold"><span style="color: #000000">lowbit</span></span><span style="color: #990000">(</span>k<span style="color: #990000">);</span> t <span style="color: #990000">*=</span> <span style="color: #993399">2</span><span style="color: #990000">)</span>
                                        bit<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">][</span>k<span style="color: #990000">]</span> <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>bit<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">][</span>k<span style="color: #990000">],</span> bit<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">][</span>k <span style="color: #990000">-</span> t<span style="color: #990000">]);</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">reverse_map_index</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> piece<span style="color: #990000">,</span> <span style="color: #009900">int</span> k<span style="color: #990000">,</span> <span style="color: #009900">int</span> re<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> re <span style="color: #990000">+</span> <span style="color: #990000">(</span>k <span style="color: #990000">-</span> <span style="color: #990000">(</span>re <span style="color: #990000">!=</span> <span style="color: #993399">0</span><span style="color: #990000">))</span> <span style="color: #990000">*</span> piece<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">map_index</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> p<span style="color: #990000">,</span> <span style="color: #009900">int</span> d<span style="color: #990000">,</span> <span style="color: #009900">int</span> re<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>p <span style="color: #990000">-</span> re<span style="color: #990000">)</span> <span style="color: #990000">/</span> d <span style="color: #990000">+</span> <span style="color: #990000">(</span>re <span style="color: #990000">!=</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">bit_update</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> piece<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">,</span> <span style="color: #009900">int</span> p<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> n <span style="color: #990000">=</span> bit<span style="color: #990000">[</span>piece<span style="color: #990000">][</span>r<span style="color: #990000">][</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
        bit<span style="color: #990000">[</span>piece<span style="color: #990000">][</span>r<span style="color: #990000">][</span>p<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span>
<span style="font-style: italic"><span style="color: #9A1900">//      std::cout &lt;&lt; "n = " &lt;&lt; n &lt;&lt; " r = " &lt;&lt; r &lt;&lt; " p = " &lt;&lt; p &lt;&lt; " v = " &lt;&lt; v &lt;&lt; '\n';</span></span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> p<span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i <span style="color: #990000">+=</span> <span style="font-weight: bold"><span style="color: #000000">lowbit</span></span><span style="color: #990000">(</span>i<span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
<span style="font-style: italic"><span style="color: #9A1900">//              std::cout &lt;&lt; "i = " &lt;&lt; i &lt;&lt; "   n = " &lt;&lt; n &lt;&lt; "    r = " &lt;&lt; r &lt;&lt; "  piece = " &lt;&lt; piece &lt;&lt; '\n';</span></span>
                bit<span style="color: #990000">[</span>piece<span style="color: #990000">][</span>r<span style="color: #990000">][</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> da<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">reverse_map_index</span></span><span style="color: #990000">(</span>piece<span style="color: #990000">,</span> i<span style="color: #990000">,</span> r<span style="color: #990000">)];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> <span style="font-weight: bold"><span style="color: #000000">lowbit</span></span><span style="color: #990000">(</span>i<span style="color: #990000">);</span> j <span style="color: #990000">*=</span> <span style="color: #993399">2</span><span style="color: #990000">)</span>
                        bit<span style="color: #990000">[</span>piece<span style="color: #990000">][</span>r<span style="color: #990000">][</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>bit<span style="color: #990000">[</span>piece<span style="color: #990000">][</span>r<span style="color: #990000">][</span>i<span style="color: #990000">],</span> bit<span style="color: #990000">[</span>piece<span style="color: #990000">][</span>r<span style="color: #990000">][</span>i <span style="color: #990000">-</span> j<span style="color: #990000">]);</span>

                <span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                for (int ti = 1; ti &lt;= bit[1][0][0]; ti++)</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                        std::cout &lt;&lt; bit[1][0][ti] &lt;&lt; ' ';</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                std::cout &lt;&lt; '\n';</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                */</span></span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> p<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        da<span style="color: #990000">[</span>p<span style="color: #990000">]</span> <span style="color: #990000">+=</span> v<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> piece<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> r <span style="color: #990000">=</span> p <span style="color: #990000">%</span> i<span style="color: #990000">;</span>
                <span style="color: #009900">int</span> index <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">map_index</span></span><span style="color: #990000">(</span>p<span style="color: #990000">,</span> i<span style="color: #990000">,</span> r<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">bit_update</span></span><span style="color: #990000">(</span>i<span style="color: #990000">,</span> r<span style="color: #990000">,</span> index<span style="color: #990000">,</span> da<span style="color: #990000">[</span>p<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">bit_query_max_to_last</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> piece<span style="color: #990000">,</span> <span style="color: #009900">int</span> re<span style="font-style: italic"><span style="color: #9A1900">/*mainder*/</span></span><span style="color: #990000">,</span> <span style="color: #009900">int</span> l<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> r <span style="color: #990000">=</span> bit<span style="color: #990000">[</span>piece<span style="color: #990000">][</span>re<span style="color: #990000">][</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
        <span style="color: #009900">int</span> ret <span style="color: #990000">=</span> da<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">reverse_map_index</span></span><span style="color: #990000">(</span>piece<span style="color: #990000">,</span> r<span style="color: #990000">,</span> re<span style="color: #990000">)];</span>

        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> da<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">reverse_map_index</span></span><span style="color: #990000">(</span>piece<span style="color: #990000">,</span> r<span style="color: #990000">,</span> re<span style="color: #990000">)]);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">==</span> r<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>r<span style="color: #990000">--;</span> r <span style="color: #990000">-</span> l <span style="color: #990000">&gt;=</span> <span style="font-weight: bold"><span style="color: #000000">lowbit</span></span><span style="color: #990000">(</span>r<span style="color: #990000">);</span> r <span style="color: #990000">-=</span> <span style="font-weight: bold"><span style="color: #000000">lowbit</span></span><span style="color: #990000">(</span>r<span style="color: #990000">))</span>
                        ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> bit<span style="color: #990000">[</span>piece<span style="color: #990000">][</span>re<span style="color: #990000">][</span>r<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ret<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">query</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x0<span style="color: #990000">,</span> <span style="color: #009900">int</span> d<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>d <span style="color: #990000">&gt;</span> piece<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> ret <span style="color: #990000">=</span> da<span style="color: #990000">[</span>x0<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> x0 <span style="color: #990000">+</span> d<span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i <span style="color: #990000">+=</span> d<span style="color: #990000">)</span>
                        ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> da<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ret<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
        <span style="color: #009900">int</span> r <span style="color: #990000">=</span> x0 <span style="color: #990000">%</span> d<span style="color: #990000">;</span>
        <span style="color: #009900">int</span> p <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">map_index</span></span><span style="color: #990000">(</span>x0<span style="color: #990000">,</span> d<span style="color: #990000">,</span> r<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">bit_query_max_to_last</span></span><span style="color: #990000">(</span>d<span style="color: #990000">,</span> r<span style="color: #990000">,</span> p<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">);</span>
        piece <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sqrt</span></span><span style="color: #990000">(</span>n<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">log</span></span><span style="color: #990000">(</span>n<span style="color: #990000">)/</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">log</span></span><span style="color: #990000">(</span><span style="color: #993399">2</span><span style="color: #990000">));</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>da<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
        <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">();</span>

        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>m<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> op<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>op<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>op <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> p<span style="color: #990000">,</span> v<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>p<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>v<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>p<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> x0<span style="color: #990000">,</span> d<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x0<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>d<span style="color: #990000">);</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">query</span></span><span style="color: #990000">(</span>x0<span style="color: #990000">,</span> d<span style="color: #990000">));</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_data_structure_poj2985_sbt_deduplicate_cc_code">2. <code>data_structure/poj2985_sbt_deduplicate.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// opt: 0</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// flag: -g</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run-: valgrind --leak-check=full $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="font-weight: bold"><span style="color: #000080">#define</span></span> NDEBUG
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cassert&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">node</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>

        <span style="color: #008080">value_type</span> value<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> size<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> size_all<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> count<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> left<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> right<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="font-style: italic"><span style="color: #9A1900">// equal element handled separately.</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">size_balanced_tree</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #000000">size_balanced_tree</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> max_size<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">max_size</span></span><span style="color: #990000">(</span>max_size<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                sbt<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>max_size<span style="color: #990000">);</span>
                next_free<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">reserve</span></span><span style="color: #990000">(</span>max_size<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> max_size<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> next_free<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>i<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
                current_free <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                root <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">allocate</span></span><span style="color: #990000">();</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">,</span> <span style="color: #009900">bool</span> flag<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                sbt<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>size_all <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>count <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">insert_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> value<span style="color: #990000">,</span> flag<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> value<span style="color: #990000">).</span>value<span style="color: #990000">;</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>
        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">select_kth_small</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> k<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_small_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> k<span style="color: #990000">);</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>
        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">select_kth_large</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> k<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_large_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> k<span style="color: #990000">);</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">rank</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">rank_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> value<span style="color: #990000">);</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">pred</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// less &lt;</span></span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">pred_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> value<span style="color: #990000">)].</span>value<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">succ</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// greater &gt;</span></span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">succ_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> value<span style="color: #990000">)].</span>value<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">get_min</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> x<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">=</span> root<span style="color: #990000">;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">;</span> x <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">get_max</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> x<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">=</span> root<span style="color: #990000">;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">;</span> x <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> x <span style="color: #990000">=</span> root<span style="color: #990000">;</span> x<span style="color: #990000">;</span> <span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">==</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span> x <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>                                      x <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">()</span>
<span style="font-weight: bold"><span style="color: #000080">#ifdef</span></span> NDEBUG
        <span style="color: #FF0000">{}</span>
<span style="font-weight: bold"><span style="color: #000080">#else</span></span>
        <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"in-tree: "</span> <span style="color: #990000">&lt;&lt;</span> <span style="font-weight: bold"><span style="color: #000000">root_value</span></span><span style="color: #990000">()</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">(</span>root<span style="color: #990000">);</span>
                std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>x<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">);</span>
                        std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"("</span> <span style="color: #990000">&lt;&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">" : "</span> <span style="color: #990000">&lt;&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">", "</span> <span style="color: #990000">&lt;&lt;</span>
                                <span style="color: #FF0000">"sall: "</span> <span style="color: #990000">&lt;&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"  size: "</span> <span style="color: #990000">&lt;&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">")  "</span> <span style="color: #990000">&lt;&lt;</span>
                                        sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>value <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span> <span style="color: #990000">&lt;&lt;</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>value <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">root_value</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span>root<span style="color: #990000">].</span>value<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #000080">#endif</span></span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>

        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">allocate</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> tmp <span style="color: #990000">=</span> current_free<span style="color: #990000">;</span>
                current_free <span style="color: #990000">=</span> next_free<span style="color: #990000">[</span>current_free<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> tmp<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">deallocate</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                next_free<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> current_free<span style="color: #990000">;</span>
                current_free <span style="color: #990000">=</span> x<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">left_rot</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> y <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>left<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>left <span style="color: #990000">=</span> x<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>size_all <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">].</span>size <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">].</span>size_all <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">].</span>size_all <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count <span style="color: #990000">&gt;=</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                x <span style="color: #990000">=</span> y<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">right_rot</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> y <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>right<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>right <span style="color: #990000">=</span> x<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>size_all <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">].</span>size <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">].</span>size_all <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">].</span>size_all <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count <span style="color: #990000">&gt;=</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                x <span style="color: #990000">=</span> y<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> <span style="color: #009900">bool</span> flag<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>flag<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>right <span style="color: #990000">].</span>size <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">left_rot</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>left <span style="color: #990000">].</span>size <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #000000">right_rot</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">);</span>
                                <span style="font-weight: bold"><span style="color: #000000">left_rot</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>left <span style="color: #990000">].</span>size <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>size<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">right_rot</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">].</span>right <span style="color: #990000">].</span>size <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>size<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #000000">left_rot</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">);</span>
                                <span style="font-weight: bold"><span style="color: #000000">right_rot</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// flag = true: value in tree, false: no value in tree</span></span>
        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">insert_detail</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">,</span> <span style="color: #009900">bool</span> flag<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span> <span style="color: #990000">&lt;=</span> x <span style="color: #990000">&amp;&amp;</span> x <span style="color: #990000">&lt;=</span> max_size<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>x<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        x <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">allocate</span></span><span style="color: #990000">();</span>
                        sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                        sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                        sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">=</span> value<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all<span style="color: #990000">++;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>flag<span style="color: #990000">)</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">++;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">insert_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> value<span style="color: #990000">,</span> flag<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">insert_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> value<span style="color: #990000">,</span> flag<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count<span style="color: #990000">++;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>flag<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> value <span style="color: #990000">&gt;=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">node&lt;value_type&gt;</span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">,</span> <span style="color: #009900">bool</span> flag <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span> <span style="color: #990000">&lt;=</span> x <span style="color: #990000">&amp;&amp;</span> x <span style="color: #990000">&lt;=</span> max_size<span style="color: #990000">);</span>
                <span style="color: #008080">node&lt;value_type&gt;</span> d<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>flag<span style="color: #990000">)</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">--;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">((</span>value <span style="color: #990000">==</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span> <span style="color: #990000">||</span> <span style="color: #990000">(</span>value <span style="color: #990000">&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">!</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">)</span>
                                <span style="color: #990000">||</span> <span style="color: #990000">(</span>value <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">!</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        d <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>flag<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                d<span style="color: #990000">.</span>count <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count<span style="color: #990000">--;</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                        sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all<span style="color: #990000">--;</span>
                                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> d<span style="color: #990000">;</span>
                                <span style="color: #FF0000">}</span>
                                <span style="font-style: italic"><span style="color: #9A1900">// delete one time lead to node disapper</span></span>
                                d<span style="color: #990000">.</span>size <span style="color: #990000">=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
                                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">--;</span>
                        <span style="color: #FF0000">}</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">&amp;&amp;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="color: #008080">node&lt;value_type&gt;</span> tmp <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">];</span>
                                sbt<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size <span style="color: #990000">=</span> tmp<span style="color: #990000">.</span>size<span style="color: #990000">;</span>
                                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all <span style="color: #990000">=</span> tmp<span style="color: #990000">.</span>size_all<span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #000000">deallocate</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                                x <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span>
                        d <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> value<span style="color: #990000">,</span> flag<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        d <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> value<span style="color: #990000">,</span> flag<span style="color: #990000">);</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size_all <span style="color: #990000">-=</span> d<span style="color: #990000">.</span>count<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>flag <span style="color: #990000">&amp;&amp;</span> d<span style="color: #990000">.</span>size <span style="color: #990000">&lt;</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">--;</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span>d<span style="color: #990000">.</span>value <span style="color: #990000">&gt;=</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> d<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">select_small_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> k<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span> <span style="color: #990000">&lt;=</span> x <span style="color: #990000">&amp;&amp;</span> x <span style="color: #990000">&lt;=</span> max_size<span style="color: #990000">);</span>
                <span style="color: #008080">size_type</span> l <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size_all <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="color: #008080">size_type</span> r <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size_all <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">&lt;=</span> k <span style="color: #990000">&amp;&amp;</span> k <span style="color: #990000">&lt;=</span> r<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>r <span style="color: #990000">&lt;</span> k<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_small_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> k <span style="color: #990000">-</span> r<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_small_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> k<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">select_large_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> k<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span> <span style="color: #990000">&lt;=</span> x <span style="color: #990000">&amp;&amp;</span> x <span style="color: #990000">&lt;=</span> max_size<span style="color: #990000">);</span>
                <span style="color: #008080">size_type</span> l <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>size_all <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="color: #008080">size_type</span> r <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>size_all <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>count<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">&lt;=</span> k <span style="color: #990000">&amp;&amp;</span> k <span style="color: #990000">&lt;=</span> r<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>r <span style="color: #990000">&lt;</span> k<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_large_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> k <span style="color: #990000">-</span> r<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_large_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> k<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">rank_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// FIXME</span></span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">rank_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">rank_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> value<span style="color: #990000">)</span> <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">pred_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> y<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>x<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> y<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&lt;</span> value<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">pred_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> x<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">pred_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> y<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">succ_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> y<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>x<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> y<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&gt;</span> value<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">succ_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> x<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">succ_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> y<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">size_type</span> max_size<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> root<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> current_free<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;node&lt;value_type&gt; &gt;</span> sbt<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> next_free<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">200007</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> father<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> size<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">get_father</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">==</span> father<span style="color: #990000">[</span>x<span style="color: #990000">])</span> <span style="color: #990000">?</span> x <span style="color: #990000">:</span> <span style="color: #990000">(</span>father<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get_father</span></span><span style="color: #990000">(</span>father<span style="color: #990000">[</span>x<span style="color: #990000">]));</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">set_union</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">,</span> <span style="color: #009900">int</span> y<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        size<span style="color: #990000">[</span>father<span style="color: #990000">[</span>x<span style="color: #990000">]]</span> <span style="color: #990000">+=</span> size<span style="color: #990000">[</span>father<span style="color: #990000">[</span>y<span style="color: #990000">]];</span>
        father<span style="color: #990000">[</span>y<span style="color: #990000">]</span> <span style="color: #990000">=</span> father<span style="color: #990000">[</span>x<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span>ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        <span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&gt;&gt;</span> m<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span> father<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">;</span> size<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #008080">size_balanced_tree&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">tree</span></span><span style="color: #990000">(</span>n<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">);</span>
        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">();</span>

        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> opt<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> opt<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>opt<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> x<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">select_kth_large</span></span><span style="color: #990000">(</span>x<span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> x <span style="color: #990000">&gt;&gt;</span> y<span style="color: #990000">;</span>
                        <span style="color: #009900">int</span> f1 <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get_father</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="color: #009900">int</span> f2 <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get_father</span></span><span style="color: #990000">(</span>y<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>f1 <span style="color: #990000">==</span> f2<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span>size<span style="color: #990000">[</span>f1<span style="color: #990000">]);</span>
                        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span>size<span style="color: #990000">[</span>f2<span style="color: #990000">]);</span>
                        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span>size<span style="color: #990000">[</span>f1<span style="color: #990000">]</span> <span style="color: #990000">+</span> size<span style="color: #990000">[</span>f2<span style="color: #990000">],</span> tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>size<span style="color: #990000">[</span>f1<span style="color: #990000">]</span> <span style="color: #990000">+</span> size<span style="color: #990000">[</span>f2<span style="color: #990000">]));</span>
                        <span style="font-weight: bold"><span style="color: #000000">set_union</span></span><span style="color: #990000">(</span>f1<span style="color: #990000">,</span> f2<span style="color: #990000">);</span>

                        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">();</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_data_structure_size_balanced_tree_v0_contain_equal_element_cc_code">3. <code>data_structure/size_balanced_tree_v0(contain_equal_element).cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// poj 2985</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意： 给定n个集合，每次可以将某两个集合合并，也可以询问第k大的集合的大小。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       暂时TLE。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// opt: 0</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// flag: -g</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run-: valgrind --leak-check=full $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="font-weight: bold"><span style="color: #000080">#define</span></span> NDEBUG
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cassert&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">node</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>

        <span style="color: #008080">value_type</span> value<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> size<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> left<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> right<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="font-style: italic"><span style="color: #9A1900">// equal element handled separately.</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">size_balanced_tree</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #000000">size_balanced_tree</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> max_size<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">max_size</span></span><span style="color: #990000">(</span>max_size<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                sbt<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>max_size<span style="color: #990000">);</span>
                next_free<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">reserve</span></span><span style="color: #990000">(</span>max_size<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> max_size<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> next_free<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>i<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
                current_free <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                root <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">allocate</span></span><span style="color: #990000">();</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #000000">insert_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> value<span style="color: #990000">);</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>
        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> value<span style="color: #990000">);</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>
        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">select_kth_small</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> k<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_small_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> k<span style="color: #990000">);</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>
        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">select_kth_large</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> k<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_large_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> k<span style="color: #990000">);</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">rank</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">rank_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> value<span style="color: #990000">);</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">pred</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// less &lt;</span></span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">pred_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> value<span style="color: #990000">)].</span>value<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">succ</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> value<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// greater &gt;</span></span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">succ_detail</span></span><span style="color: #990000">(</span>root<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> value<span style="color: #990000">)].</span>value<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">get_min</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> x<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">=</span> root<span style="color: #990000">;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">;</span> x <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">get_max</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> x<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">=</span> root<span style="color: #990000">;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">;</span> x <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">()</span>
<span style="font-weight: bold"><span style="color: #000080">#ifdef</span></span> NDEBUG
        <span style="color: #FF0000">{}</span>
<span style="font-weight: bold"><span style="color: #000080">#else</span></span>
        <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"in-tree: </span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">(</span>root<span style="color: #990000">);</span>
                std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>x<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">);</span>
                        std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #000080">#endif</span></span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>

        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">allocate</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> tmp <span style="color: #990000">=</span> current_free<span style="color: #990000">;</span>
                current_free <span style="color: #990000">=</span> next_free<span style="color: #990000">[</span>current_free<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> tmp<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">deallocate</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                next_free<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> current_free<span style="color: #990000">;</span>
                current_free <span style="color: #990000">=</span> x<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">left_rot</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> y <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>left<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>left <span style="color: #990000">=</span> x<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">].</span>size <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                x <span style="color: #990000">=</span> y<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">right_rot</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> y <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>right<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>right <span style="color: #990000">=</span> x<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>y<span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">].</span>size <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                x <span style="color: #990000">=</span> y<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> <span style="color: #009900">bool</span> flag<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>flag<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>right <span style="color: #990000">].</span>size <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">left_rot</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>left <span style="color: #990000">].</span>size <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #000000">right_rot</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">);</span>
                                <span style="font-weight: bold"><span style="color: #000000">left_rot</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>left <span style="color: #990000">].</span>size <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>size<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">right_rot</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">].</span>right <span style="color: #990000">].</span>size <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>size<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #000000">left_rot</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">);</span>
                                <span style="font-weight: bold"><span style="color: #000000">right_rot</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">insert_detail</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span> <span style="color: #990000">&lt;=</span> x <span style="color: #990000">&amp;&amp;</span> x <span style="color: #990000">&lt;=</span> max_size<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>x<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        x <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">allocate</span></span><span style="color: #990000">();</span>
                        sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                        sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                        sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">=</span> value<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">++;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">insert_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                        <span style="font-style: italic"><span style="color: #9A1900">// for equal element, we insert_detail it size_type right subtree</span></span>
                        <span style="font-style: italic"><span style="color: #9A1900">// to ensure to correctness of find-kth small element.</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #000000">insert_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">maintain</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> value <span style="color: #990000">&gt;=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>size_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span> <span style="color: #990000">&lt;=</span> x <span style="color: #990000">&amp;&amp;</span> x <span style="color: #990000">&lt;=</span> max_size<span style="color: #990000">);</span>
                <span style="color: #008080">value_type</span> d_value<span style="color: #990000">;</span>
                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>size<span style="color: #990000">--;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">((</span>value <span style="color: #990000">==</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span> <span style="color: #990000">||</span> <span style="color: #990000">(</span>value <span style="color: #990000">&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">!</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">)</span>
                                <span style="color: #990000">||</span> <span style="color: #990000">(</span>value <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">!</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        d_value <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">&amp;&amp;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">)</span>
                                sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #000000">deallocate</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                                x <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span>
                        d_value <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        d_value <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">remove_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span>d_value <span style="color: #990000">&gt;=</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> d_value<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">select_small_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> k<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span> <span style="color: #990000">&lt;=</span> x <span style="color: #990000">&amp;&amp;</span> x <span style="color: #990000">&lt;=</span> max_size<span style="color: #990000">);</span>
                <span style="color: #008080">size_type</span> r <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>r <span style="color: #990000">==</span> k<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>r <span style="color: #990000">&lt;</span> k<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_small_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> k <span style="color: #990000">-</span> r<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_small_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> k<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">select_large_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> k<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span> <span style="color: #990000">&lt;=</span> x <span style="color: #990000">&amp;&amp;</span> x <span style="color: #990000">&lt;=</span> max_size<span style="color: #990000">);</span>
                <span style="color: #008080">size_type</span> r <span style="color: #990000">=</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right <span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>r <span style="color: #990000">==</span> k<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>r <span style="color: #990000">&lt;</span> k<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_large_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> k <span style="color: #990000">-</span> r<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">select_large_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> k<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">rank_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&lt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">rank_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>value <span style="color: #990000">&gt;</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">rank_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> value<span style="color: #990000">)</span> <span style="color: #990000">+</span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sbt<span style="color: #990000">[</span> sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left <span style="color: #990000">].</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">pred_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> y<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>x<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> y<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&lt;</span> value<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">pred_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> x<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">pred_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> y<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">succ_detail</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> y<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> value<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>x<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> y<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>value <span style="color: #990000">&gt;</span> value<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">succ_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>right<span style="color: #990000">,</span> x<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">succ_detail</span></span><span style="color: #990000">(</span>sbt<span style="color: #990000">[</span>x<span style="color: #990000">].</span>left<span style="color: #990000">,</span> y<span style="color: #990000">,</span> value<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">size_type</span> max_size<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> root<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> current_free<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;node&lt;value_type&gt; &gt;</span> sbt<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> next_free<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">200007</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> father<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> size<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">get_father</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">==</span> father<span style="color: #990000">[</span>x<span style="color: #990000">])</span> <span style="color: #990000">?</span> x <span style="color: #990000">:</span> <span style="color: #990000">(</span>father<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get_father</span></span><span style="color: #990000">(</span>father<span style="color: #990000">[</span>x<span style="color: #990000">]));</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">set_union</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">,</span> <span style="color: #009900">int</span> y<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        size<span style="color: #990000">[</span>father<span style="color: #990000">[</span>x<span style="color: #990000">]]</span> <span style="color: #990000">+=</span> size<span style="color: #990000">[</span>father<span style="color: #990000">[</span>y<span style="color: #990000">]];</span>
        father<span style="color: #990000">[</span>y<span style="color: #990000">]</span> <span style="color: #990000">=</span> father<span style="color: #990000">[</span>x<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span>ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        <span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&gt;&gt;</span> m<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span> father<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">;</span> size<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #008080">size_balanced_tree&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">tree</span></span><span style="color: #990000">(</span>n<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">);</span>

        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> opt<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> opt<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>opt<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> x<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">select_kth_large</span></span><span style="color: #990000">(</span>x<span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> x <span style="color: #990000">&gt;&gt;</span> y<span style="color: #990000">;</span>
                        <span style="color: #009900">int</span> f1 <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get_father</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                        <span style="color: #009900">int</span> f2 <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get_father</span></span><span style="color: #990000">(</span>y<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>f1 <span style="color: #990000">==</span> f2<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span>size<span style="color: #990000">[</span>f1<span style="color: #990000">]);</span>
                        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span>size<span style="color: #990000">[</span>f2<span style="color: #990000">]);</span>
                        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span>size<span style="color: #990000">[</span>f1<span style="color: #990000">]</span> <span style="color: #990000">+</span> size<span style="color: #990000">[</span>f2<span style="color: #990000">]);</span>
                        <span style="font-weight: bold"><span style="color: #000000">set_union</span></span><span style="color: #990000">(</span>f1<span style="color: #990000">,</span> f2<span style="color: #990000">);</span>

                        tree<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">inorder</span></span><span style="color: #990000">();</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_data_structure_sparse_table_v0_dynamic_cc_code">4. <code>data_structure/sparse_table_v0_dynamic.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;functional&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cmath&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">,</span>
        <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">COMPARE</span> <span style="color: #990000">=</span> std<span style="color: #990000">::</span>less<span style="color: #990000">&lt;</span>T<span style="color: #990000">&gt;</span>
<span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">sparse_table</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">unsigned</span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">COMPARE</span> comparer<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;value_type&gt;</span> vector_type<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">inline</span></span> <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">log2</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> len<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">log</span></span><span style="color: #990000">(</span>len<span style="color: #990000">)/</span><span style="font-weight: bold"><span style="color: #000000">log</span></span><span style="color: #990000">(</span><span style="color: #993399">2</span><span style="color: #990000">);</span>
                <span style="color: #008080">size_type</span> tmp <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> <span style="font-weight: bold"><span style="color: #000000">size_type</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> tmp<span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> len<span style="color: #990000">;</span> tmp<span style="color: #990000">++);</span> <span style="font-style: italic"><span style="color: #9A1900">// FIXME 11 neednot write size_type</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> tmp <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="font-weight: bold"><span style="color: #000000">sparse_table</span></span><span style="color: #990000">(</span><span style="color: #008080">vector_type</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> data<span style="color: #990000">,</span> <span style="color: #008080">comparer</span> cmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">comparer</span></span><span style="color: #990000">())</span>
                <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">data</span></span><span style="color: #990000">(</span>data<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>cmp<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> n <span style="color: #990000">=</span> data<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
                <span style="color: #008080">size_type</span> len <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">log2</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                st<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">typename</span></span> <span style="color: #008080">std</span><span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span>vector_type<span style="color: #990000">&gt;::</span><span style="color: #008080">iterator</span> it <span style="color: #990000">=</span> st<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span>
                                it <span style="color: #990000">!=</span> st<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>it<span style="color: #990000">)</span>
                        <span style="color: #990000">(*</span>it<span style="color: #990000">).</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>len <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">size_type</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> st<span style="color: #990000">[</span>i<span style="color: #990000">][</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">size_type</span> l <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> l <span style="color: #990000">&lt;=</span> len<span style="color: #990000">;</span> l<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">size_type</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #990000">(</span>i <span style="color: #990000">+</span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span>l<span style="color: #990000">)</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>data<span style="color: #990000">[</span> st<span style="color: #990000">[</span>i<span style="color: #990000">][</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">],</span> data<span style="color: #990000">[</span> st<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;(</span>l<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">))][</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">]))</span>
                                        st<span style="color: #990000">[</span>i<span style="color: #990000">][</span>l<span style="color: #990000">]</span> <span style="color: #990000">=</span> st<span style="color: #990000">[</span>i<span style="color: #990000">][</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                        st<span style="color: #990000">[</span>i<span style="color: #990000">][</span>l<span style="color: #990000">]</span> <span style="color: #990000">=</span> st<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #990000">(</span><span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #990000">(</span>l<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">))][</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// [l, r]</span></span>
        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">query_interval_index</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> l<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> r<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> k <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">log2</span></span><span style="color: #990000">(</span>r <span style="color: #990000">-</span> l <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>data<span style="color: #990000">[</span> st<span style="color: #990000">[</span>l<span style="color: #990000">][</span>k<span style="color: #990000">]</span> <span style="color: #990000">],</span> data<span style="color: #990000">[</span> st<span style="color: #990000">[</span>r <span style="color: #990000">-</span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span>k<span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">][</span>k<span style="color: #990000">]</span> <span style="color: #990000">]))</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> st<span style="color: #990000">[</span>l<span style="color: #990000">][</span>k<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> st<span style="color: #990000">[</span>r <span style="color: #990000">-</span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span>k<span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">][</span>k<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// [l, r]</span></span>
        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">query_interval</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> l<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> r<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> data<span style="color: #990000">[</span> <span style="font-weight: bold"><span style="color: #000000">query_interval_index</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> r<span style="color: #990000">)</span> <span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;vector_type&gt;</span> st<span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// store the index of the dest value</span></span>
        <span style="color: #008080">vector_type</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> data<span style="color: #990000">;</span>
        <span style="color: #008080">comparer</span> cmp<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> v<span style="color: #FF0000">{</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> <span style="color: #993399">3</span><span style="color: #990000">,</span> <span style="color: #993399">4</span><span style="color: #990000">,</span> <span style="color: #993399">5</span><span style="color: #FF0000">}</span><span style="color: #990000">;</span>
        <span style="color: #008080">sparse_table&lt;int, std::greater&lt;int&gt;&gt;</span> <span style="font-weight: bold"><span style="color: #000000">st</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> st<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">query_interval</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #993399">4</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> st<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">query_interval_index</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #993399">4</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_data_structure_sparse_table_v0_static_cc_code">5. <code>data_structure/sparse_table_v0_static.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">/* poj3368</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> *</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * 给定一个单调数列，求区间众数。</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> *</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * solution</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * 算出每个数出现次数，然后对于一个区间，由于是单调的，两边的连续相同的数单独处理，</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * 对于中间的数做一次rmq就ok。这里封装st的模板。</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * poj不开-o2, 真恶心。</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> *</span></span>
<span style="font-style: italic"><span style="color: #9A1900">*/</span></span>

<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;functional&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">,</span>
        <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">COMPARE</span> <span style="color: #990000">=</span> std<span style="color: #990000">::</span>less<span style="color: #990000">&lt;</span>T<span style="color: #990000">&gt;</span>
<span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">sparse_table</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">unsigned</span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">COMPARE</span> comparer<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;value_type&gt;</span> vector_type<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">inline</span></span> <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">log2</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> len<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> tmp <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> <span style="font-weight: bold"><span style="color: #000000">size_type</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> tmp<span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> len<span style="color: #990000">;</span> tmp<span style="color: #990000">++);</span> <span style="font-style: italic"><span style="color: #9A1900">// FIXME 11 neednot write size_type</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> tmp <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="font-weight: bold"><span style="color: #000000">sparse_table</span></span><span style="color: #990000">(</span><span style="color: #008080">comparer</span> cmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">comparer</span></span><span style="color: #990000">())</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>cmp<span style="color: #990000">)</span> <span style="color: #FF0000">{}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">(</span><span style="color: #008080">vector_type</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> data<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> n <span style="color: #990000">=</span> data<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
                <span style="color: #008080">size_type</span> len <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">log2</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">size_type</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> st<span style="color: #990000">[</span>i<span style="color: #990000">][</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">size_type</span> l <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> l <span style="color: #990000">&lt;=</span> len<span style="color: #990000">;</span> l<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">size_type</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #990000">(</span>i <span style="color: #990000">+</span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span>l<span style="color: #990000">)</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>data<span style="color: #990000">[</span> st<span style="color: #990000">[</span>i<span style="color: #990000">][</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">],</span> data<span style="color: #990000">[</span> st<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;(</span>l<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">))][</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">]))</span>
                                        st<span style="color: #990000">[</span>i<span style="color: #990000">][</span>l<span style="color: #990000">]</span> <span style="color: #990000">=</span> st<span style="color: #990000">[</span>i<span style="color: #990000">][</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                        st<span style="color: #990000">[</span>i<span style="color: #990000">][</span>l<span style="color: #990000">]</span> <span style="color: #990000">=</span> st<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #990000">(</span><span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #990000">(</span>l<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">))][</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// [l, r]</span></span>
        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">query_interval_index</span></span><span style="color: #990000">(</span><span style="color: #008080">vector_type</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> data<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> l<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> r<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> k <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">log2</span></span><span style="color: #990000">(</span>r <span style="color: #990000">-</span> l <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>data<span style="color: #990000">[</span> st<span style="color: #990000">[</span>l<span style="color: #990000">][</span>k<span style="color: #990000">]</span> <span style="color: #990000">],</span> data<span style="color: #990000">[</span> st<span style="color: #990000">[</span>r <span style="color: #990000">-</span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span>k<span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">][</span>k<span style="color: #990000">]</span> <span style="color: #990000">]))</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> st<span style="color: #990000">[</span>l<span style="color: #990000">][</span>k<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> st<span style="color: #990000">[</span>r <span style="color: #990000">-</span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span>k<span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">][</span>k<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// [l, r]</span></span>
        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">query_interval</span></span><span style="color: #990000">(</span><span style="color: #008080">vector_type</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> data<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> l<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> r<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> data<span style="color: #990000">[</span> <span style="font-weight: bold"><span style="color: #000000">query_interval_index</span></span><span style="color: #990000">(</span>data<span style="color: #990000">,</span> l<span style="color: #990000">,</span> r<span style="color: #990000">)</span> <span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
        size_type <span style="font-weight: bold"><span style="color: #0000FF">static</span></span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">100007</span><span style="color: #990000">;</span>
        size_type <span style="font-weight: bold"><span style="color: #0000FF">static</span></span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span>  len <span style="color: #990000">=</span> <span style="color: #993399">17</span><span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> st<span style="color: #990000">[</span>maxn<span style="color: #990000">][</span>len<span style="color: #990000">];</span>
        <span style="color: #008080">comparer</span> cmp<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>


<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxl <span style="color: #990000">=</span> <span style="color: #993399">200007</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">100007</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> dis <span style="color: #990000">=</span> <span style="color: #993399">100000</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> count<span style="color: #990000">[</span>maxl<span style="color: #990000">];</span>
<span style="color: #009900">int</span> da<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> left<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> right<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> q<span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">)</span> <span style="color: #990000">!=</span> EOF <span style="color: #990000">&amp;&amp;</span> n<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>q<span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">num</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>count<span style="color: #990000">,</span> count <span style="color: #990000">+</span> maxl<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>da<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
                        count<span style="color: #990000">[</span>da<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> dis<span style="color: #990000">]++;</span>
                <span style="color: #FF0000">}</span>
                right<span style="color: #990000">[</span>n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> n<span style="color: #990000">-</span><span style="color: #993399">2</span><span style="color: #990000">;</span> i <span style="color: #990000">&gt;=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i<span style="color: #990000">--)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>da<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> da<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">])</span> right<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> right<span style="color: #990000">[</span>i<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>                                    right<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">;</span>
                left<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>da<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> da<span style="color: #990000">[</span>i <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">])</span> left<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> left<span style="color: #990000">[</span>i<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>                                    left<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> num<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> count<span style="color: #990000">[</span>da<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> dis<span style="color: #990000">];</span>
                <span style="color: #008080">sparse_table&lt;int, std::greater&lt;int&gt; &gt;</span> st<span style="color: #990000">;</span>
                st<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">(</span>num<span style="color: #990000">);</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> l<span style="color: #990000">,</span> r<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> q<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>l<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>r<span style="color: #990000">);</span>
                        l<span style="color: #990000">--;</span> r<span style="color: #990000">--;</span>
                        <span style="color: #009900">int</span> tl <span style="color: #990000">=</span> right<span style="color: #990000">[</span>l<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> tr <span style="color: #990000">=</span> left<span style="color: #990000">[</span>r<span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tl <span style="color: #990000">&gt;</span> r <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> tl <span style="color: #990000">=</span> r <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tr <span style="color: #990000">&lt;</span> l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> tr <span style="color: #990000">=</span> l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                        <span style="color: #009900">int</span> max <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>r <span style="color: #990000">-</span> tr<span style="color: #990000">,</span> tl <span style="color: #990000">-</span> l<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tl <span style="color: #990000">&lt;=</span> tr<span style="color: #990000">)</span>
                                max <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>max<span style="color: #990000">,</span> st<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">query_interval</span></span><span style="color: #990000">(</span>num<span style="color: #990000">,</span> tl<span style="color: #990000">,</span> tr<span style="color: #990000">));</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> max<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_data_structure_splay_v0_cc_code">6. <code>data_structure/splay_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// poj3580</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：很直接，给定一个长度为n的数列，支持单点插入，单点删除，区间增加，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       区间翻转，区间旋转，然后询问区间最小值。n和操作个数都&lt;=100000。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：splay可以很方便支持这些操作。区间增加和区间翻转只需要打个lazy tag就行。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       区间旋转的话其实本质是讲某个区间删除然后插入到另一个区间后面。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       可作为splay模板。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 统计：0.844s，coding time too much, too much tried</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: time -p $exec &lt; bfdiff.in</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// flag: -g</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cassert&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>

<span style="color: #009900">long</span> <span style="color: #009900">long</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">300100</span><span style="color: #990000">;</span>
<span style="color: #009900">long</span> <span style="color: #009900">long</span> a<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">long</span> <span style="color: #009900">long</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">30</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> splay_tree
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">node</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">node</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> size <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> min <span style="color: #990000">=</span> value <span style="color: #990000">=</span> inf<span style="color: #990000">;</span> delta <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> rev <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
                <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> c<span style="color: #990000">,</span> <span style="color: #009900">long</span> <span style="color: #009900">long</span> is_right<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> child<span style="color: #990000">[</span>is_right<span style="color: #990000">]</span> <span style="color: #990000">=</span> c<span style="color: #990000">;</span> c<span style="color: #990000">-&gt;</span>parent <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">this</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
                <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">this</span></span> <span style="color: #990000">==</span> parent<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span> <span style="color: #FF0000">}</span>
                <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span><span style="color: #009900">long</span> <span style="color: #009900">long</span> d<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> delta <span style="color: #990000">+=</span> d<span style="color: #990000">;</span> min <span style="color: #990000">+=</span> d<span style="color: #990000">;</span> value <span style="color: #990000">+=</span> d<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
                <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">reverse</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> rev <span style="color: #990000">^=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>

                <span style="color: #009900">long</span> <span style="color: #009900">long</span> size<span style="color: #990000">,</span> value<span style="color: #990000">,</span> min<span style="color: #990000">,</span> delta<span style="color: #990000">;</span>
                <span style="color: #009900">bool</span> rev<span style="color: #990000">;</span>
                node <span style="color: #990000">*</span> child<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">],</span> <span style="color: #990000">*</span> parent<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span> <span style="color: #990000">*</span> root<span style="color: #990000">,</span> memory<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> <span style="color: #990000">*</span> nil<span style="color: #990000">,</span> <span style="color: #990000">*</span> alloc <span style="color: #990000">=</span> memory<span style="color: #990000">;</span>

        node <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">node_alloc</span></span><span style="color: #990000">(</span><span style="color: #009900">long</span> <span style="color: #009900">long</span> v<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                alloc<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> alloc<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
                alloc<span style="color: #990000">-&gt;</span>size <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                alloc<span style="color: #990000">-&gt;</span>value <span style="color: #990000">=</span> v<span style="color: #990000">;</span>
                alloc<span style="color: #990000">-&gt;</span>min <span style="color: #990000">=</span> v<span style="color: #990000">;</span>
                alloc<span style="color: #990000">-&gt;</span>delta <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                alloc<span style="color: #990000">-&gt;</span>rev <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> alloc<span style="color: #990000">++;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">relax</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> t<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>delta<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">long</span> <span style="color: #009900">long</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> nil<span style="color: #990000">)</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>i<span style="color: #990000">]-&gt;</span><span style="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>delta<span style="color: #990000">);</span>
                        t<span style="color: #990000">-&gt;</span>delta <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>rev<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">long</span> <span style="color: #009900">long</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> nil<span style="color: #990000">)</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>i<span style="color: #990000">]-&gt;</span><span style="font-weight: bold"><span style="color: #000000">reverse</span></span><span style="color: #990000">();</span>
                        t<span style="color: #990000">-&gt;</span>rev <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> t<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                t<span style="color: #990000">-&gt;</span>size <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]-&gt;</span>size <span style="color: #990000">+</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]-&gt;</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                t<span style="color: #990000">-&gt;</span>min <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>value<span style="color: #990000">,</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]-&gt;</span>min<span style="color: #990000">,</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]-&gt;</span>min<span style="color: #990000">));</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> t<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> p <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">relax</span></span><span style="color: #990000">(</span>p<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">relax</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="color: #009900">bool</span> is_right <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">();</span>
                p<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>t<span style="color: #990000">,</span> p<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">());</span>
                p<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[!</span>is_right<span style="color: #990000">],</span> is_right<span style="color: #990000">);</span>
                t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>p<span style="color: #990000">,</span> <span style="color: #990000">!</span>is_right<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>p<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>p <span style="color: #990000">==</span> root<span style="color: #990000">)</span> root <span style="color: #990000">=</span> t<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> t<span style="color: #990000">,</span> node <span style="color: #990000">*</span> parent<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>parent <span style="color: #990000">!=</span> parent<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">-&gt;</span>parent <span style="color: #990000">==</span> parent<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">()</span> <span style="color: #990000">==</span> t<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">())</span> <span style="color: #990000">?</span>
                                        <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">))</span> <span style="color: #990000">:</span>
                                                <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">));</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> t<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">,</span> nil<span style="color: #990000">);</span> <span style="color: #FF0000">}</span>

        node <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">build</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">&gt;=</span> r<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> nil<span style="color: #990000">;</span>
                <span style="color: #009900">int</span> mid <span style="color: #990000">=</span> <span style="color: #990000">(</span>l <span style="color: #990000">+</span> r<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
                node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">node_alloc</span></span><span style="color: #990000">(</span>a<span style="color: #990000">[</span>mid<span style="color: #990000">]);</span>
                t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">build</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> mid<span style="color: #990000">),</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">build</span></span><span style="color: #990000">(</span>mid <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> r<span style="color: #990000">),</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> t<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">(</span><span style="color: #009900">long</span> <span style="color: #009900">long</span> n<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                nil <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">new</span></span> node<span style="color: #990000">;</span>
                root <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">build</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> n <span style="color: #990000">+</span> <span style="color: #993399">2</span><span style="color: #990000">);</span> root<span style="color: #990000">-&gt;</span>parent <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        node <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span><span style="color: #009900">long</span> <span style="color: #009900">long</span> k<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> root<span style="color: #990000">;</span> <span style="color: #990000">;</span> <span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #000000">relax</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                        <span style="color: #009900">long</span> <span style="color: #009900">long</span> c <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]-&gt;</span>size<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>k <span style="color: #990000">==</span> c<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> t<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>k <span style="color: #990000">&gt;</span> c<span style="color: #990000">)</span>
                                k <span style="color: #990000">-=</span> c <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> t <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                t <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// interval operations</span></span>

        node <span style="color: #990000">*</span> <span style="color: #990000">&amp;</span> <span style="font-weight: bold"><span style="color: #000000">get</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// [l, r)</span></span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> tl <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                node <span style="color: #990000">*</span> tr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>r<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tl<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> tl<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> tr<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">interval_add</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">,</span> <span style="color: #009900">int</span> d<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> <span style="color: #990000">&amp;</span> t <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> r <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span>d<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">reverse</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> <span style="color: #990000">&amp;</span> t <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> r <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">reverse</span></span><span style="color: #990000">();</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">long</span> <span style="color: #009900">long</span> <span style="font-weight: bold"><span style="color: #000000">query</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> <span style="color: #990000">&amp;</span> t <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> r <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> t<span style="color: #990000">-&gt;</span>min<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">,</span> <span style="color: #009900">int</span> p<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>p<span style="color: #990000">);</span>
                node <span style="color: #990000">*</span> tr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>p <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
                node <span style="color: #990000">*</span> tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">node_alloc</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                tmp<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                tr<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> p<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>p <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                node <span style="color: #990000">*</span> tr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>p <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
                tr<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">revolve</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">,</span> <span style="color: #009900">int</span> shift<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> len <span style="color: #990000">=</span> r <span style="color: #990000">-</span> l <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                shift <span style="color: #990000">=</span> <span style="color: #990000">(</span>shift <span style="color: #990000">%</span> len <span style="color: #990000">+</span> len<span style="color: #990000">)</span> <span style="color: #990000">%</span> len<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>shift<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span>

                <span style="font-style: italic"><span style="color: #9A1900">// [l, r - shift], [r - shift + 1, r]</span></span>
                node <span style="color: #990000">*</span> tl <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                node <span style="color: #990000">*</span> tr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>r <span style="color: #990000">-</span> shift <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                node <span style="color: #990000">*</span> txr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>r<span style="color: #990000">);</span>
                node <span style="color: #990000">*</span> txrr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span>r <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tl<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> tl<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">relax</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>
                node <span style="color: #990000">*</span> tt <span style="color: #990000">=</span> tr<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
                tr<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>

                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>txr<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>txrr<span style="color: #990000">,</span> txr<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">relax</span></span><span style="color: #990000">(</span>txrr<span style="color: #990000">);</span>

                txrr<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>tt<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>txrr<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>txrr<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%lld"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>a<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
        a<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> a<span style="color: #990000">[</span>n <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> a<span style="color: #990000">[</span>n <span style="color: #990000">+</span> <span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> inf<span style="color: #990000">;</span>
        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>m<span style="color: #990000">);</span>
        <span style="color: #009900">char</span> command<span style="color: #990000">[</span><span style="color: #993399">10</span><span style="color: #990000">];</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">long</span> <span style="color: #009900">long</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%s"</span><span style="color: #990000">,</span> command<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">strcmp</span></span><span style="color: #990000">(</span>command<span style="color: #990000">,</span> <span style="color: #FF0000">"ADD"</span><span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> l<span style="color: #990000">,</span> r<span style="color: #990000">,</span> d<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>l<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>r<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>d<span style="color: #990000">);</span>
                        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">interval_add</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> r<span style="color: #990000">,</span> d<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">strcmp</span></span><span style="color: #990000">(</span>command<span style="color: #990000">,</span> <span style="color: #FF0000">"REVERSE"</span><span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> l<span style="color: #990000">,</span> r<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>l<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>r<span style="color: #990000">);</span>
                        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">reverse</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> r<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">strcmp</span></span><span style="color: #990000">(</span>command<span style="color: #990000">,</span> <span style="color: #FF0000">"REVOLVE"</span><span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> l<span style="color: #990000">,</span> r<span style="color: #990000">,</span> t<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>l<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>r<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>t<span style="color: #990000">);</span>
                        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">revolve</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> r<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">strcmp</span></span><span style="color: #990000">(</span>command<span style="color: #990000">,</span> <span style="color: #FF0000">"INSERT"</span><span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> x<span style="color: #990000">,</span> p<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>p<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">);</span>
                        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> p<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">strcmp</span></span><span style="color: #990000">(</span>command<span style="color: #990000">,</span> <span style="color: #FF0000">"DELETE"</span><span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> p<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>p<span style="color: #990000">);</span>
                        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span>p<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">strcmp</span></span><span style="color: #990000">(</span>command<span style="color: #990000">,</span> <span style="color: #FF0000">"MIN"</span><span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> l<span style="color: #990000">,</span> r<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>l<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>r<span style="color: #990000">);</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%lld</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">query</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> r<span style="color: #990000">));</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_data_structure_splay_v1_cc_code">7. <code>data_structure/splay_v1.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// cf-100796-j</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：给定一辆车，n个操作，可以从前门进一个人，后门进一个人，第i个</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       上车的人要下车，选择从前门或者后门人少的下，然后前面的人先从</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       对应的门下去，再从另一个门上去。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：经典splay。提高效率用prev和succ存每个人前驱和后继，然后维护下。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 统计：63ms</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// opt: 0</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// flag: -g</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">100100</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> tot<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> splay_tree
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">node</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">node</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> size <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
                <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> c<span style="color: #990000">,</span> <span style="color: #009900">int</span> is_right<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> child<span style="color: #990000">[</span>is_right<span style="color: #990000">]</span> <span style="color: #990000">=</span> c<span style="color: #990000">;</span> c<span style="color: #990000">-&gt;</span>parent <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">this</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
                <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">this</span></span> <span style="color: #990000">==</span> parent<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span> <span style="color: #FF0000">}</span>

                <span style="color: #009900">int</span> size<span style="color: #990000">;</span>
                node <span style="color: #990000">*</span> child<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">],</span> <span style="color: #990000">*</span> parent<span style="color: #990000">,</span> <span style="color: #990000">*</span> succ<span style="color: #990000">,</span> <span style="color: #990000">*</span> prev<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span> <span style="color: #990000">*</span> root<span style="color: #990000">,</span> memory<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> <span style="color: #990000">*</span> nil<span style="color: #990000">,</span> <span style="color: #990000">*</span> alloc <span style="color: #990000">=</span> memory<span style="color: #990000">,</span> <span style="color: #990000">*</span> first<span style="color: #990000">,</span> <span style="color: #990000">*</span> last<span style="color: #990000">;</span>

        node <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">node_alloc</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                alloc<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> alloc<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
                alloc<span style="color: #990000">-&gt;</span>size <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> alloc<span style="color: #990000">++;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> t<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                t<span style="color: #990000">-&gt;</span>size <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]-&gt;</span>size <span style="color: #990000">+</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]-&gt;</span>size <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> t<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> p <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">;</span>
                <span style="color: #009900">bool</span> is_right <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">();</span>
                p<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>t<span style="color: #990000">,</span> p<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">());</span>
                p<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[!</span>is_right<span style="color: #990000">],</span> is_right<span style="color: #990000">);</span>
                t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>p<span style="color: #990000">,</span> <span style="color: #990000">!</span>is_right<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>p<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>p <span style="color: #990000">==</span> root<span style="color: #990000">)</span> root <span style="color: #990000">=</span> t<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> t<span style="color: #990000">,</span> node <span style="color: #990000">*</span> parent<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>parent <span style="color: #990000">!=</span> parent<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">-&gt;</span>parent <span style="color: #990000">==</span> parent<span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">()</span> <span style="color: #990000">==</span> t<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">is_right</span></span><span style="color: #990000">())</span> <span style="color: #990000">?</span>
                                        <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>parent<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">))</span> <span style="color: #990000">:</span>
                                                <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">rot</span></span><span style="color: #990000">(</span>t<span style="color: #990000">));</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> t<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">,</span> nil<span style="color: #990000">);</span> <span style="color: #FF0000">}</span>

        node <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">build</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">&gt;=</span> r<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> nil<span style="color: #990000">;</span>
                <span style="color: #009900">int</span> mid <span style="color: #990000">=</span> <span style="color: #990000">(</span>l <span style="color: #990000">+</span> r<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
                node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">node_alloc</span></span><span style="color: #990000">();</span>
                t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">build</span></span><span style="color: #990000">(</span>l<span style="color: #990000">,</span> mid<span style="color: #990000">),</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                t<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">build</span></span><span style="color: #990000">(</span>mid <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> r<span style="color: #990000">),</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> t<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                nil <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">new</span></span> node<span style="color: #990000">;</span>
                root <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">build</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">);</span> root<span style="color: #990000">-&gt;</span>parent <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
                first <span style="color: #990000">=</span> memory <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> last <span style="color: #990000">=</span> memory<span style="color: #990000">;</span>
                first<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> last<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
                first<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> last<span style="color: #990000">;</span>
                last<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> first<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        node <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">select</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> k<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> root<span style="color: #990000">;</span> <span style="color: #990000">;</span> <span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> c <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]-&gt;</span>size<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>k <span style="color: #990000">==</span> c<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> t<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>k <span style="color: #990000">&gt;</span> c<span style="color: #990000">)</span>
                                k <span style="color: #990000">-=</span> c <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> t <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                t <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">insert_last</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> last<span style="color: #990000">-&gt;</span>prev<span style="color: #990000">;</span>
                node <span style="color: #990000">*</span> tr <span style="color: #990000">=</span> last<span style="color: #990000">;</span>
                node <span style="color: #990000">*</span> tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">node_alloc</span></span><span style="color: #990000">();</span>

                tmp<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> t<span style="color: #990000">;</span>
                tmp<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> tr<span style="color: #990000">;</span>
                t<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> tmp<span style="color: #990000">;</span>
                tr<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> tmp<span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> t<span style="color: #990000">);</span>

                tmp<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                tr<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">insert_first</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> first<span style="color: #990000">;</span>
                node <span style="color: #990000">*</span> tr <span style="color: #990000">=</span> first<span style="color: #990000">-&gt;</span>succ<span style="color: #990000">;</span>
                node <span style="color: #990000">*</span> tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">node_alloc</span></span><span style="color: #990000">();</span>

                tmp<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> t<span style="color: #990000">;</span>
                tmp<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> tr<span style="color: #990000">;</span>
                t<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> tmp<span style="color: #990000">;</span>
                tr<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> tmp<span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> t<span style="color: #990000">);</span>

                tmp<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                tr<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>


        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span>node <span style="color: #990000">*</span> x<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> x<span style="color: #990000">-&gt;</span>prev<span style="color: #990000">;</span>
                node <span style="color: #990000">*</span> tr <span style="color: #990000">=</span> x<span style="color: #990000">-&gt;</span>succ<span style="color: #990000">;</span>

                t<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> tr<span style="color: #990000">;</span>
                tr<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> t<span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> t<span style="color: #990000">);</span>

                tr<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">remove_query</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> p<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                node <span style="color: #990000">*</span> t <span style="color: #990000">=</span> memory <span style="color: #990000">+</span> <span style="color: #993399">2</span> <span style="color: #990000">+</span> p<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="color: #009900">int</span> left <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]-&gt;</span>size <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> right <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]-&gt;</span>size <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>left <span style="color: #990000">&lt;</span> right<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> left<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>first<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
                        node <span style="color: #990000">*</span> tl <span style="color: #990000">=</span> first<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
                        first<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
                        node <span style="color: #990000">*</span> tlast <span style="color: #990000">=</span> last<span style="color: #990000">-&gt;</span>prev<span style="color: #990000">;</span>

                        tlast<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> first<span style="color: #990000">-&gt;</span>succ<span style="color: #990000">;</span>
                        first<span style="color: #990000">-&gt;</span>succ<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> tlast<span style="color: #990000">;</span>
                        last<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>prev<span style="color: #990000">;</span>
                        t<span style="color: #990000">-&gt;</span>prev<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> last<span style="color: #990000">;</span>
                        t<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> first<span style="color: #990000">;</span>
                        first<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> t<span style="color: #990000">;</span>

                        <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tlast<span style="color: #990000">);</span>

                        tlast<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>tl<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>tlast<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]);</span>
                        <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tlast<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]);</span>
                        <span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> right<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>last<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
                        node <span style="color: #990000">*</span> tl <span style="color: #990000">=</span> last<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
                        last<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> nil<span style="color: #990000">;</span>
                        node <span style="color: #990000">*</span> tfirst <span style="color: #990000">=</span> first<span style="color: #990000">-&gt;</span>succ<span style="color: #990000">;</span>

                        tfirst<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> last<span style="color: #990000">-&gt;</span>prev<span style="color: #990000">;</span>
                        last<span style="color: #990000">-&gt;</span>prev<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> tfirst<span style="color: #990000">;</span>
                        first<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> t<span style="color: #990000">-&gt;</span>succ<span style="color: #990000">;</span>
                        t<span style="color: #990000">-&gt;</span>succ<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> first<span style="color: #990000">;</span>
                        t<span style="color: #990000">-&gt;</span>succ <span style="color: #990000">=</span> last<span style="color: #990000">;</span>
                        last<span style="color: #990000">-&gt;</span>prev <span style="color: #990000">=</span> t<span style="color: #990000">;</span>

                        <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tfirst<span style="color: #990000">);</span>

                        tfirst<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]-&gt;</span><span style="font-weight: bold"><span style="color: #000000">set_child</span></span><span style="color: #990000">(</span>tl<span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>tfirst<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]);</span>
                        <span style="font-weight: bold"><span style="color: #000000">splay</span></span><span style="color: #990000">(</span>tfirst<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]);</span>
                        <span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">(</span>t<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">);</span>
        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">();</span>
        <span style="color: #009900">char</span> command<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">];</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%s"</span><span style="color: #990000">,</span> command<span style="color: #990000">);</span>

<span style="font-weight: bold"><span style="color: #000080">#ifdef</span></span> __DEBUG
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"-----------------------------&gt; "</span> <span style="color: #990000">&lt;&lt;</span> i <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> tot<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> j <span style="color: #990000">+</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">" prev: "</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #990000">((</span>splay_tree<span style="color: #990000">::</span>memory <span style="color: #990000">+</span> <span style="color: #993399">2</span> <span style="color: #990000">+</span> j<span style="color: #990000">)-&gt;</span>prev <span style="color: #990000">-</span> splay_tree<span style="color: #990000">::</span>memory<span style="color: #990000">)</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span>
                                <span style="color: #FF0000">"  succ: "</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #990000">((</span>splay_tree<span style="color: #990000">::</span>memory <span style="color: #990000">+</span> <span style="color: #993399">2</span> <span style="color: #990000">+</span> j<span style="color: #990000">)-&gt;</span>succ <span style="color: #990000">-</span> splay_tree<span style="color: #990000">::</span>memory<span style="color: #990000">)</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #000080">#endif</span></span>

                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>command<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #FF0000">'F'</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">insert_last</span></span><span style="color: #990000">();</span>
                        tot<span style="color: #990000">++;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>command<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #FF0000">'B'</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">insert_first</span></span><span style="color: #990000">();</span>
                        tot<span style="color: #990000">++;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>command<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #FF0000">'O'</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> id<span style="color: #990000">;</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>id<span style="color: #990000">);</span>
                        splay_tree<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">remove_query</span></span><span style="color: #990000">(--</span>id<span style="color: #990000">);</span>
                        tot<span style="color: #990000">--;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_dynamic_programming_optimization_poj1742_cc_code">8. <code>dynamic_programming/optimization/poj1742.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// 题意：给定n个硬币面值Ai和相应个数Ci，问最多可以凑出多少个1～m的价值。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       n &lt;= 100, m &lt;= 100000, Ci &lt;= 1000。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：最暴力的背包就是O(n*m*C)，但是会超时，事实上转移的时候只会和</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       j, j - Ai, j - 2*Ai, ... , j - Ci*Ai有关，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       这其中如果有一个是可行的，那么对于前i个硬币，面值为j就一定可行。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       维护一个tot数组表示与j在同一个模Ai剩余系的可行的个数，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       每次看tot[j] - tot[j - Ci*Ai]是不是大于零，如果是j也可行。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       这样总的复杂度降到O(n*m)。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: time $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;utility&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">107</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxm <span style="color: #990000">=</span> <span style="color: #993399">100007</span><span style="color: #990000">;</span>

<span style="color: #009900">bool</span> f<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">][</span>maxm<span style="color: #990000">];</span>
<span style="color: #009900">int</span> tot<span style="color: #990000">[</span>maxm<span style="color: #990000">];</span>
std<span style="color: #990000">::</span><span style="color: #008080">pair&lt;int, int&gt;</span> da<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">,</span> circle<span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span>ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&gt;&gt;</span> m <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">(</span>n <span style="color: #990000">||</span> m<span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>first<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>second<span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> f<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">][</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> f<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">][</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
                f<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">][</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                <span style="color: #009900">int</span> current <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> prev <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        current <span style="color: #990000">^=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> prev <span style="color: #990000">=</span> current <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                        circle <span style="color: #990000">=</span> da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>first <span style="color: #990000">*</span> <span style="color: #990000">(</span>da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>second <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;=</span> m<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                                tot<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> f<span style="color: #990000">[</span>current<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> f<span style="color: #990000">[</span>prev<span style="color: #990000">][</span>j<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>j <span style="color: #990000">&gt;=</span> da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>first<span style="color: #990000">)</span> tot<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">+=</span> tot<span style="color: #990000">[</span>j <span style="color: #990000">-</span> da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>first<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>f<span style="color: #990000">[</span>current<span style="color: #990000">][</span>j<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                                <span style="color: #009900">int</span> tmp <span style="color: #990000">=</span> tot<span style="color: #990000">[</span>j<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>j <span style="color: #990000">&gt;=</span> circle<span style="color: #990000">)</span> tmp <span style="color: #990000">-=</span> tot<span style="color: #990000">[</span>j <span style="color: #990000">-</span> circle<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tmp<span style="color: #990000">)</span> f<span style="color: #990000">[</span>current<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="color: #009900">int</span> ans <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> ans <span style="color: #990000">+=</span> f<span style="color: #990000">[</span>current<span style="color: #990000">][</span>i<span style="color: #990000">];</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> ans <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_geometry_arc_polar_angle_cc_code">9. <code>geometry/arc-polar-angle.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// 题意：给定n(n &lt;= 300)个点，求一个半径为1的圆最多能覆盖多少点。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：O(n^3)的做法就是枚举两点（覆盖最多点的圆肯定可以有两个点在边界上），</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       然后O(n)求个数。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       good。然后比较厉害的是这个O(n^2*log(n))的做法，对于每个点，O(n)</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       求出其他所有和它距离小于等于2的点在与它相交部分的圆弧（在枚举点上）。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       保存圆弧按极角序，然后按极角序排序。在枚举，遇到起点+1, 终点-1,</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       不断更新答案，然后 答案+1 的最大值就是结果。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 统计：896ms, 34min, 1A</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// std: c++03</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cmath&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;utility&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">pair&lt;double, double&gt;</span> point_type<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">pair&lt;double, double&gt;</span> arc_type<span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">303</span><span style="color: #990000">;</span>
<span style="color: #008080">point_type</span> point<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
std<span style="color: #990000">::</span><span style="color: #008080">pair&lt;double, int&gt;</span> arc<span style="color: #990000">[</span><span style="color: #993399">4</span><span style="color: #990000">*</span>maxn<span style="color: #990000">];</span> <span style="font-style: italic"><span style="color: #9A1900">// polar angle and tag(for start or end)</span></span>

<span style="color: #009900">int</span> n<span style="color: #990000">,</span> ans<span style="color: #990000">;</span>

<span style="color: #009900">double</span> <span style="font-weight: bold"><span style="color: #000000">sqr</span></span><span style="color: #990000">(</span><span style="color: #009900">double</span> x<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> x <span style="color: #990000">*</span> x<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
<span style="color: #009900">double</span> <span style="font-weight: bold"><span style="color: #000000">get_dis</span></span><span style="color: #990000">(</span><span style="color: #008080">point_type</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> a<span style="color: #990000">,</span> <span style="color: #008080">point_type</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sqrt</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">sqr</span></span><span style="color: #990000">(</span>a<span style="color: #990000">.</span>first <span style="color: #990000">-</span> b<span style="color: #990000">.</span>first<span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #000000">sqr</span></span><span style="color: #990000">(</span>a<span style="color: #990000">.</span>second <span style="color: #990000">-</span> b<span style="color: #990000">.</span>second<span style="color: #990000">));</span>
<span style="color: #FF0000">}</span>

<span style="color: #008080">arc_type</span> <span style="font-weight: bold"><span style="color: #000000">get_arc_intersection</span></span><span style="color: #990000">(</span><span style="color: #008080">point_type</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> a<span style="color: #990000">,</span> <span style="color: #008080">point_type</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">double</span> base <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">atan2</span></span><span style="color: #990000">((</span>a<span style="color: #990000">.</span>second <span style="color: #990000">-</span> b<span style="color: #990000">.</span>second<span style="color: #990000">),</span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>first <span style="color: #990000">-</span> b<span style="color: #990000">.</span>first<span style="color: #990000">));</span>
        <span style="color: #009900">double</span> delta <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">acos</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">get_dis</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> b<span style="color: #990000">)/</span><span style="color: #993399">2.0</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">arc_type</span></span><span style="color: #990000">(</span>base <span style="color: #990000">-</span> delta<span style="color: #990000">,</span> base <span style="color: #990000">+</span> delta<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span>ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&amp;&amp;</span> n<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                ans <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> point<span style="color: #990000">[</span>i<span style="color: #990000">].</span>first <span style="color: #990000">&gt;&gt;</span> point<span style="color: #990000">[</span>i<span style="color: #990000">].</span>second<span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> tot <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>j <span style="color: #990000">==</span> i <span style="color: #990000">||</span> <span style="font-weight: bold"><span style="color: #000000">get_dis</span></span><span style="color: #990000">(</span>point<span style="color: #990000">[</span>i<span style="color: #990000">],</span> point<span style="color: #990000">[</span>j<span style="color: #990000">])</span> <span style="color: #990000">&gt;</span> <span style="color: #993399">2</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                                <span style="color: #008080">arc_type</span> tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">get_arc_intersection</span></span><span style="color: #990000">(</span>point<span style="color: #990000">[</span>i<span style="color: #990000">],</span> point<span style="color: #990000">[</span>j<span style="color: #990000">]);</span>
                                arc<span style="color: #990000">[</span>tot<span style="color: #990000">].</span>first <span style="color: #990000">=</span> tmp<span style="color: #990000">.</span>first<span style="color: #990000">;</span> arc<span style="color: #990000">[</span>tot<span style="color: #990000">++].</span>second <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                                arc<span style="color: #990000">[</span>tot<span style="color: #990000">].</span>first <span style="color: #990000">=</span> tmp<span style="color: #990000">.</span>second<span style="color: #990000">;</span> arc<span style="color: #990000">[</span>tot<span style="color: #990000">++].</span>second <span style="color: #990000">=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>arc<span style="color: #990000">,</span> arc <span style="color: #990000">+</span> tot<span style="color: #990000">);</span>

                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> count <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> tot<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                                count <span style="color: #990000">+=</span> arc<span style="color: #990000">[</span>j<span style="color: #990000">].</span>second<span style="color: #990000">;</span>
                                ans <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ans<span style="color: #990000">,</span> count<span style="color: #990000">);</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> ans <span style="color: #990000">+</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_geometry_convex_hull_level_v0_cc_code">10. <code>geometry/convex-hull-level_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// opt: 0</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// flag: -g</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// std: c++03</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">point</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">point</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> y<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">x</span></span><span style="color: #990000">(</span>x<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">y</span></span><span style="color: #990000">(</span>y<span style="color: #990000">)</span> <span style="color: #FF0000">{}</span>
        <span style="font-weight: bold"><span style="color: #000000">point</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{}</span>
        <span style="color: #008080">value_type</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span>
        <span style="color: #009900">long</span> <span style="color: #009900">long</span> index<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> cord_type<span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;point&lt;cord_type&gt; &gt;</span> points<span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;point&lt;cord_type&gt; &gt;</span> stack<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">signum</span></span><span style="color: #990000">(</span><span style="color: #008080">T</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> x<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">&lt;</span> <span style="color: #993399">0</span> <span style="color: #990000">?</span> <span style="color: #990000">-</span><span style="color: #993399">1</span> <span style="color: #990000">:</span> x <span style="color: #990000">&gt;</span> <span style="color: #993399">0</span> <span style="color: #990000">?</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span> <span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>


<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="color: #008080">T</span> <span style="font-weight: bold"><span style="color: #000000">ccw</span></span><span style="color: #990000">(</span><span style="color: #008080">point&lt;T&gt; const &amp; a, point&lt;T&gt; const &amp; b, point&lt;T&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> c<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">signum</span></span><span style="color: #990000">((</span><span style="color: #009900">long</span> <span style="color: #009900">long</span><span style="color: #990000">)(</span>b<span style="color: #990000">.</span>x <span style="color: #990000">-</span> a<span style="color: #990000">.</span>x<span style="color: #990000">)</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>c<span style="color: #990000">.</span>y <span style="color: #990000">-</span> a<span style="color: #990000">.</span>y<span style="color: #990000">)</span> <span style="color: #990000">-</span> <span style="color: #990000">(</span><span style="color: #009900">long</span> <span style="color: #009900">long</span><span style="color: #990000">)(</span>b<span style="color: #990000">.</span>y <span style="color: #990000">-</span> a<span style="color: #990000">.</span>y<span style="color: #990000">)</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>c<span style="color: #990000">.</span>x <span style="color: #990000">-</span> a<span style="color: #990000">.</span>x<span style="color: #990000">));</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">cmp</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #0000FF">operator</span></span><span style="color: #990000">()(</span><span style="color: #008080">point&lt;T&gt; const &amp; a, point&lt;T&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> a<span style="color: #990000">.</span>y <span style="color: #990000">&lt;</span> b<span style="color: #990000">.</span>y <span style="color: #990000">||</span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>y <span style="color: #990000">==</span> b<span style="color: #990000">.</span>y <span style="color: #990000">&amp;&amp;</span> a<span style="color: #990000">.</span>x <span style="color: #990000">&lt;</span> b<span style="color: #990000">.</span>x<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #009900">int</span> BZ <span style="color: #990000">=</span> <span style="color: #993399">30</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">15</span><span style="color: #990000">;</span>
<span style="color: #009900">char</span> Buf<span style="color: #990000">[</span>BZ <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">],</span> <span style="color: #990000">*</span>buf <span style="color: #990000">=</span> Buf<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">inline</span></span> <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">scan</span></span><span style="color: #990000">(</span><span style="color: #008080">T</span> <span style="color: #990000">&amp;</span>a<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// method: for huge input</span></span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">bool</span> flag <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>a <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&lt;</span> <span style="color: #FF0000">'0'</span> <span style="color: #990000">||</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&gt;</span> <span style="color: #FF0000">'9'</span><span style="color: #990000">;</span> <span style="color: #990000">++</span>buf<span style="color: #990000">)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(*</span>buf <span style="color: #990000">==</span> <span style="color: #FF0000">'-'</span><span style="color: #990000">)</span> flag <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&gt;=</span> <span style="color: #FF0000">'0'</span> <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&lt;=</span> <span style="color: #FF0000">'9'</span><span style="color: #990000">;</span> buf<span style="color: #990000">++)</span>
                a <span style="color: #990000">=</span> a <span style="color: #990000">*</span> <span style="color: #993399">10</span> <span style="color: #990000">+</span> <span style="color: #990000">(*</span>buf <span style="color: #990000">-</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>flag<span style="color: #990000">)</span> a <span style="color: #990000">=</span> <span style="color: #990000">-</span>a<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fread</span></span><span style="color: #990000">(</span>Buf<span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> BZ<span style="color: #990000">,</span> stdin<span style="color: #990000">);</span>
        <span style="color: #009900">int</span> n<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #000000">scan</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span> stack<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">scan</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>x<span style="color: #990000">);</span> <span style="font-weight: bold"><span style="color: #000000">scan</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>y<span style="color: #990000">);</span>
                points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>index <span style="color: #990000">=</span> i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> cmp<span style="color: #990000">&lt;</span>cord_type<span style="color: #990000">&gt;());</span>

        <span style="color: #009900">int</span> top <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>top <span style="color: #990000">&gt;=</span> <span style="color: #993399">2</span> <span style="color: #990000">&amp;&amp;</span> <span style="font-weight: bold"><span style="color: #000000">ccw</span></span><span style="color: #990000">(</span>stack<span style="color: #990000">[</span>top <span style="color: #990000">-</span> <span style="color: #993399">2</span><span style="color: #990000">],</span> stack<span style="color: #990000">[</span>top <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">],</span> points<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #990000">&lt;</span> <span style="color: #993399">0</span><span style="color: #990000">)</span>
                        top<span style="color: #990000">--;</span>
                stack<span style="color: #990000">[</span>top<span style="color: #990000">++]</span> <span style="color: #990000">=</span> points<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>
        <span style="color: #009900">int</span> t <span style="color: #990000">=</span> top <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> n <span style="color: #990000">-</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> i <span style="color: #990000">&gt;=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i<span style="color: #990000">--)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>top <span style="color: #990000">&gt;=</span> t <span style="color: #990000">&amp;&amp;</span> <span style="font-weight: bold"><span style="color: #000000">ccw</span></span><span style="color: #990000">(</span>stack<span style="color: #990000">[</span>top <span style="color: #990000">-</span> <span style="color: #993399">2</span><span style="color: #990000">],</span> stack<span style="color: #990000">[</span>top <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">],</span> points<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #990000">&lt;</span> <span style="color: #993399">0</span><span style="color: #990000">)</span>
                        top<span style="color: #990000">--;</span>
                stack<span style="color: #990000">[</span>top<span style="color: #990000">++]</span> <span style="color: #990000">=</span> points<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// the perimeter of the convex hull</span></span>
        <span style="color: #009900">long</span> <span style="color: #009900">long</span> ans <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> top <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                ans <span style="color: #990000">=</span> <span style="color: #990000">(</span>ans <span style="color: #990000">*</span> stack<span style="color: #990000">[</span>i<span style="color: #990000">].</span>index<span style="color: #990000">)</span> <span style="color: #990000">%</span> <span style="color: #990000">(</span>n <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        ans <span style="color: #990000">=</span> <span style="color: #990000">(</span>ans <span style="color: #990000">*</span> <span style="color: #990000">(</span>top <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">))</span> <span style="color: #990000">%</span> <span style="color: #990000">(</span>n <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%lld</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> ans<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_geometry_convex_hull_polar_angle_cc_code">11. <code>geometry/convex-hull-polar-angle.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iomanip&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">point</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">using</span></span> value_type <span style="color: #990000">=</span> T<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">point</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> y<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">x</span></span><span style="color: #990000">(</span>x<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">y</span></span><span style="color: #990000">(</span>y<span style="color: #990000">)</span> <span style="color: #FF0000">{}</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">point</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{}</span><span style="color: #990000">;</span>
        <span style="color: #008080">value_type</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span>
        <span style="color: #009900">double</span> polar_angle<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #0000FF">operator</span></span><span style="color: #990000">&lt;(</span><span style="color: #008080">point&lt;T&gt; const &amp; a, point&lt;T&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>y <span style="color: #990000">&lt;</span> b<span style="color: #990000">.</span>y <span style="color: #990000">||</span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>y <span style="color: #990000">==</span> b<span style="color: #990000">.</span>y <span style="color: #990000">&amp;&amp;</span> a<span style="color: #990000">.</span>x <span style="color: #990000">&lt;</span> b<span style="color: #990000">.</span>x<span style="color: #990000">));</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
std<span style="color: #990000">::</span>istream <span style="color: #990000">&amp;</span> <span style="font-weight: bold"><span style="color: #0000FF">operator</span></span><span style="color: #990000">&gt;&gt;(</span>std<span style="color: #990000">::</span>istream <span style="color: #990000">&amp;</span> is<span style="color: #990000">,</span> point<span style="color: #990000">&lt;</span>T<span style="color: #990000">&gt;</span> <span style="color: #990000">&amp;</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        is <span style="color: #990000">&gt;&gt;</span> a<span style="color: #990000">.</span>x <span style="color: #990000">&gt;&gt;</span> a<span style="color: #990000">.</span>y<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> is<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;point&lt;double&gt;&gt;</span> points<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="color: #008080">T</span> <span style="font-weight: bold"><span style="color: #000000">ccw</span></span><span style="color: #990000">(</span><span style="color: #008080">point&lt;T&gt; const &amp; a, point&lt;T&gt; const &amp; b, point&lt;T&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> c<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>b<span style="color: #990000">.</span>x <span style="color: #990000">-</span> a<span style="color: #990000">.</span>x<span style="color: #990000">)</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>c<span style="color: #990000">.</span>y <span style="color: #990000">-</span> a<span style="color: #990000">.</span>y<span style="color: #990000">)</span> <span style="color: #990000">-</span> <span style="color: #990000">(</span>b<span style="color: #990000">.</span>y <span style="color: #990000">-</span> a<span style="color: #990000">.</span>y<span style="color: #990000">)</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>c<span style="color: #990000">.</span>x <span style="color: #990000">-</span> a<span style="color: #990000">.</span>x<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="color: #009900">double</span> <span style="font-weight: bold"><span style="color: #000000">dis</span></span><span style="color: #990000">(</span><span style="color: #008080">point&lt;T&gt; const &amp; a, point&lt;T&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">double</span> sx <span style="color: #990000">=</span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>x <span style="color: #990000">-</span> b<span style="color: #990000">.</span>x<span style="color: #990000">)</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>x <span style="color: #990000">-</span> b<span style="color: #990000">.</span>x<span style="color: #990000">);</span>
        <span style="color: #009900">double</span> sy <span style="color: #990000">=</span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>y <span style="color: #990000">-</span> b<span style="color: #990000">.</span>y<span style="color: #990000">)</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>y <span style="color: #990000">-</span> b<span style="color: #990000">.</span>y<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sqrt</span></span><span style="color: #990000">(</span>sx <span style="color: #990000">+</span> sy<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> n<span style="color: #990000">;</span> std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n<span style="color: #990000">;</span>

        points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> points<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>points<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> points<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">])</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span>i<span style="color: #990000">],</span> points<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> x <span style="color: #990000">=</span> points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>x <span style="color: #990000">-</span> points<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span>x<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> y <span style="color: #990000">=</span> points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>y <span style="color: #990000">-</span> points<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span>y<span style="color: #990000">;</span>
                points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>polar_angle <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">atan</span></span><span style="color: #990000">(</span><span style="color: #009900">double</span><span style="color: #990000">(</span>y<span style="color: #990000">)/</span><span style="color: #009900">double</span><span style="color: #990000">(</span>x<span style="color: #990000">));</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>polar_angle <span style="color: #990000">&lt;</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>polar_angle <span style="color: #990000">+=</span> M_PI<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">()+</span><span style="color: #993399">2</span><span style="color: #990000">,</span> points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="color: #990000">[](</span><span style="color: #008080">point&lt;double&gt; const &amp; a, point&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span><span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> a<span style="color: #990000">.</span>polar_angle <span style="color: #990000">&lt;</span> b<span style="color: #990000">.</span>polar_angle<span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span><span style="color: #990000">);</span>
        points<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> points<span style="color: #990000">[</span>n<span style="color: #990000">];</span>

        <span style="color: #009900">int</span> c <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">//convex points counter</span></span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> i <span style="color: #990000">&lt;=</span> n <span style="color: #990000">&amp;&amp;</span> <span style="font-weight: bold"><span style="color: #000000">ccw</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span>c<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">],</span> points<span style="color: #990000">[</span>c<span style="color: #990000">],</span> points<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #990000">)</span>
                        <span style="color: #990000">(</span>c <span style="color: #990000">&gt;</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">?</span> c<span style="color: #990000">--</span> <span style="color: #990000">:</span> i<span style="color: #990000">++;</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[++</span>c<span style="color: #990000">],</span> points<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// the perimeter of the convex hull</span></span>
        <span style="color: #009900">double</span> ans <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> c<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                ans <span style="color: #990000">+=</span> <span style="font-weight: bold"><span style="color: #000000">dis</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span>i<span style="color: #990000">],</span> points<span style="color: #990000">[</span>i<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">]);</span>

        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> std<span style="color: #990000">::</span>fixed <span style="color: #990000">&lt;&lt;</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">setprecision</span></span><span style="color: #990000">(</span><span style="color: #993399">2</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> ans <span style="color: #990000">&lt;&lt;</span><span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_geometry_convex_hull_polar_angle_v1_cc_code">12. <code>geometry/convex-hull-polar-angle_v1.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// std: c++03</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cmath&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iomanip&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">point</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">point</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> y<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">x</span></span><span style="color: #990000">(</span>x<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">y</span></span><span style="color: #990000">(</span>y<span style="color: #990000">)</span> <span style="color: #FF0000">{}</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">point</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{}</span><span style="color: #990000">;</span>
        <span style="color: #008080">value_type</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span>
        <span style="color: #009900">double</span> polar_angle<span style="color: #990000">;</span>
        <span style="color: #009900">long</span> <span style="color: #009900">long</span> index<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #0000FF">operator</span></span><span style="color: #990000">&lt;(</span><span style="color: #008080">point&lt;T&gt; const &amp; a, point&lt;T&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>y <span style="color: #990000">&lt;</span> b<span style="color: #990000">.</span>y <span style="color: #990000">||</span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span>y <span style="color: #990000">==</span> b<span style="color: #990000">.</span>y <span style="color: #990000">&amp;&amp;</span> a<span style="color: #990000">.</span>x <span style="color: #990000">&lt;</span> b<span style="color: #990000">.</span>x<span style="color: #990000">));</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
std<span style="color: #990000">::</span>istream <span style="color: #990000">&amp;</span> <span style="font-weight: bold"><span style="color: #0000FF">operator</span></span><span style="color: #990000">&gt;&gt;(</span>std<span style="color: #990000">::</span>istream <span style="color: #990000">&amp;</span> is<span style="color: #990000">,</span> point<span style="color: #990000">&lt;</span>T<span style="color: #990000">&gt;</span> <span style="color: #990000">&amp;</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        is <span style="color: #990000">&gt;&gt;</span> a<span style="color: #990000">.</span>x <span style="color: #990000">&gt;&gt;</span> a<span style="color: #990000">.</span>y<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> is<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;point&lt;long long&gt; &gt;</span> points<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="color: #008080">T</span> <span style="font-weight: bold"><span style="color: #000000">abs</span></span><span style="color: #990000">(</span><span style="color: #008080">T</span> a<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>a <span style="color: #990000">&gt;</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">?</span> a <span style="color: #990000">:</span> <span style="color: #990000">-</span>a<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="color: #008080">T</span> <span style="font-weight: bold"><span style="color: #000000">ccw</span></span><span style="color: #990000">(</span><span style="color: #008080">point&lt;T&gt; const &amp; a, point&lt;T&gt; const &amp; b, point&lt;T&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> c<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>b<span style="color: #990000">.</span>x <span style="color: #990000">-</span> a<span style="color: #990000">.</span>x<span style="color: #990000">)</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>c<span style="color: #990000">.</span>y <span style="color: #990000">-</span> a<span style="color: #990000">.</span>y<span style="color: #990000">)</span> <span style="color: #990000">-</span> <span style="color: #990000">(</span>b<span style="color: #990000">.</span>y <span style="color: #990000">-</span> a<span style="color: #990000">.</span>y<span style="color: #990000">)</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>c<span style="color: #990000">.</span>x <span style="color: #990000">-</span> a<span style="color: #990000">.</span>x<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">closer</span></span><span style="color: #990000">(</span><span style="color: #008080">point&lt;T&gt; const &amp; a, point&lt;T&gt; const &amp; b, point&lt;T&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> c<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #000000">abs</span></span><span style="color: #990000">(</span>b<span style="color: #990000">.</span>x <span style="color: #990000">-</span> a<span style="color: #990000">.</span>x<span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #000000">abs</span></span><span style="color: #990000">(</span>b<span style="color: #990000">.</span>y <span style="color: #990000">-</span> a<span style="color: #990000">.</span>y<span style="color: #990000">)</span> <span style="color: #990000">&lt;</span> <span style="font-weight: bold"><span style="color: #000000">abs</span></span><span style="color: #990000">(</span>c<span style="color: #990000">.</span>x <span style="color: #990000">-</span> a<span style="color: #990000">.</span>x<span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #000000">abs</span></span><span style="color: #990000">(</span>c<span style="color: #990000">.</span>y <span style="color: #990000">-</span> a<span style="color: #990000">.</span>y<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">cmp</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #0000FF">operator</span></span><span style="color: #990000">()(</span><span style="color: #008080">point&lt;T&gt; const &amp; a, point&lt;T&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> c <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">ccw</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">],</span> a<span style="color: #990000">,</span> b<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> c <span style="color: #990000">&gt;</span> <span style="color: #993399">0</span> <span style="color: #990000">||</span> <span style="color: #990000">(</span>c <span style="color: #990000">==</span> <span style="color: #993399">0</span> <span style="color: #990000">&amp;&amp;</span> <span style="font-weight: bold"><span style="color: #000000">closer</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">],</span> a<span style="color: #990000">,</span> b<span style="color: #990000">));</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span>ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>

        <span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        points.resize(3);</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        point&lt;long long&gt; aa(0, 0);</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        point&lt;long long&gt; bb(1, 0);</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        point&lt;long long&gt; cc(2, 0);</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        points[1] = aa;</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        std::cout &lt;&lt; cmp&lt;long long&gt;()(bb, cc) &lt;&lt; '\n';</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        std::cout &lt;&lt; ccw&lt;long long&gt;(aa, bb, cc) &lt;&lt; '\n';</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        std::cout &lt;&lt; closer&lt;long long&gt;(aa, bb, cc) &lt;&lt; '\n';</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        return 0;</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        */</span></span>

        <span style="color: #009900">long</span> <span style="color: #009900">long</span> n<span style="color: #990000">;</span> std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n<span style="color: #990000">;</span>
        points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> points<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>index <span style="color: #990000">=</span> i<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>points<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> points<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">])</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span>i<span style="color: #990000">],</span> points<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        for (int i = 2; i &lt;= n; i++) {</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                int x = points[i].x - points[1].x;</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                int y = points[i].y - points[1].y;</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                points[i].polar_angle = std::atan(double(y)/double(x));</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                if (points[i].polar_angle &lt; 0) points[i].polar_angle += M_PI;</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        }</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        */</span></span>

        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">()+</span><span style="color: #993399">2</span><span style="color: #990000">,</span> points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> cmp<span style="color: #990000">&lt;</span><span style="color: #009900">long</span> <span style="color: #009900">long</span><span style="color: #990000">&gt;());</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"pre sort: </span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>index <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"end sort</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
        points<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> points<span style="color: #990000">[</span>n<span style="color: #990000">];</span>

        <span style="color: #009900">long</span> <span style="color: #009900">long</span> c <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">//convex points counter</span></span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> i <span style="color: #990000">&lt;=</span> n <span style="color: #990000">&amp;&amp;</span> <span style="font-weight: bold"><span style="color: #000000">ccw</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span>c<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">],</span> points<span style="color: #990000">[</span>c<span style="color: #990000">],</span> points<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #990000">&lt;</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #990000">)</span>
                        <span style="color: #990000">(</span>c <span style="color: #990000">&gt;</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">?</span> c<span style="color: #990000">--</span> <span style="color: #990000">:</span> i<span style="color: #990000">++;</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[++</span>c<span style="color: #990000">],</span> points<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// the perimeter of the convex hull</span></span>
        <span style="color: #009900">long</span> <span style="color: #009900">long</span> ans <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> c<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                ans <span style="color: #990000">=</span> <span style="color: #990000">(</span>ans <span style="color: #990000">*</span> points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>index<span style="color: #990000">)</span> <span style="color: #990000">%</span> <span style="color: #990000">(</span>n <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"---&gt;"</span> <span style="color: #990000">&lt;&lt;</span> points<span style="color: #990000">[</span>i<span style="color: #990000">].</span>index <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
        ans <span style="color: #990000">=</span> <span style="color: #990000">(</span>ans <span style="color: #990000">*</span> c<span style="color: #990000">)</span> <span style="color: #990000">%</span> <span style="color: #990000">(</span>n <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> ans <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_bipartite_graph_maximum_matching_hungarian_v0_cc_code">13. <code>graph/ bipartite-graph-maximum-matching-hungarian_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">//sgu242</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">using</span></span> <span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> std<span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">610</span><span style="color: #990000">;</span>
<span style="color: #008080">vector&lt;vector&lt;int&gt; &gt;</span> graph<span style="color: #990000">;</span>
<span style="color: #009900">int</span> linked<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">bool</span> visited<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> k<span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> vector<span style="color: #990000">&lt;</span><span style="color: #009900">int</span><span style="color: #990000">&gt;::</span><span style="color: #008080">iterator</span> iter_edge<span style="color: #990000">;</span>
<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">hungarian_dfs</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">iter_edge</span> it <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> it <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> it<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> v <span style="color: #990000">=</span> <span style="color: #990000">*</span>it<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>visited<span style="color: #990000">[</span>v<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                        visited<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>linked<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #990000">-</span><span style="color: #993399">1</span> <span style="color: #990000">||</span> <span style="font-weight: bold"><span style="color: #000000">hungarian_dfs</span></span><span style="color: #990000">(</span>linked<span style="color: #990000">[</span>v<span style="color: #990000">]))</span> <span style="color: #FF0000">{</span>
                                linked<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span>
                                linked<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> u<span style="color: #990000">;</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">print_plan</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="color: #008080">vector&lt;vector&lt;int&gt; &gt;</span> ans_plan<span style="color: #990000">;</span>
        ans_plan<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>k<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>linked<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #009900">int</span> t <span style="color: #990000">=</span> linked<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">-</span> n<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t <span style="color: #990000">&gt;=</span> k<span style="color: #990000">)</span> t<span style="color: #990000">-=</span> k<span style="color: #990000">;</span>
                ans_plan<span style="color: #990000">[</span>t<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>i<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> k<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                cout <span style="color: #990000">&lt;&lt;</span> ans_plan<span style="color: #990000">[</span>i<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>vector<span style="color: #990000">&lt;</span><span style="color: #009900">int</span><span style="color: #990000">&gt;::</span><span style="color: #008080">iterator</span> it <span style="color: #990000">=</span> ans_plan<span style="color: #990000">[</span>i<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span>
                                it <span style="color: #990000">!=</span> ans_plan<span style="color: #990000">[</span>i<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> it<span style="color: #990000">++)</span>
                        cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">" "</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #990000">*</span>it<span style="color: #990000">;</span>
                cout <span style="color: #990000">&lt;&lt;</span> endl<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&gt;&gt;</span> k<span style="color: #990000">;</span>
        <span style="color: #009900">int</span> node_num <span style="color: #990000">=</span> n <span style="color: #990000">+</span> <span style="color: #993399">2</span><span style="color: #990000">*</span>k<span style="color: #990000">;</span>
        graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>node_num<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> t<span style="color: #990000">;</span>
                cin <span style="color: #990000">&gt;&gt;</span> t<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> tmp<span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> t<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        cin <span style="color: #990000">&gt;&gt;</span> tmp<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>i<span style="color: #990000">,</span> n <span style="color: #990000">+</span> tmp<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>i<span style="color: #990000">,</span> n <span style="color: #990000">+</span> k <span style="color: #990000">+</span> tmp<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">int</span> bi_match <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>linked<span style="color: #990000">,</span> linked <span style="color: #990000">+</span> node_num<span style="color: #990000">,</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>visited<span style="color: #990000">,</span> visited <span style="color: #990000">+</span> node_num<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">hungarian_dfs</span></span><span style="color: #990000">(</span>i<span style="color: #990000">))</span> bi_match<span style="color: #990000">++;</span>
        <span style="color: #FF0000">}</span>

        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>bi_match <span style="color: #990000">==</span> <span style="color: #993399">2</span><span style="color: #990000">*</span>k<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"YES"</span> <span style="color: #990000">&lt;&lt;</span> endl<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">print_plan</span></span><span style="color: #990000">();</span>

        <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"NO"</span> <span style="color: #990000">&lt;&lt;</span> endl<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_bipartite_graph_maximum_matching_hungarian_v1_cc_code">14. <code>graph/ bipartite-graph-maximum-matching-hungarian_v1.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// un test</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">bipartite_graph</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;size_type&gt;</span> edge_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span>size_type<span style="color: #990000">&gt;::</span><span style="color: #008080">iterator</span> edge_iter<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;edge_type&gt;</span> graph_type<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #000000">bipartite_graph</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> n<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// [0, n)</span></span>
                                                                 <span style="font-style: italic"><span style="color: #9A1900">// for even node black, {0, 2, 4, ...}</span></span>
                                                                 <span style="font-style: italic"><span style="color: #9A1900">//      odd node white, {1, 3, 5, ...}</span></span>
        <span style="color: #FF0000">{</span>
                graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                linked<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                visited<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> y<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                graph<span style="color: #990000">[</span>x<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>y<span style="color: #990000">);</span> graph<span style="color: #990000">[</span>y<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">hungarian_dfs</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> u<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">edge_iter</span> it <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> it <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>it<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #008080">size_type</span> v <span style="color: #990000">=</span> <span style="color: #990000">*</span>it<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>visited<span style="color: #990000">[</span>v<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                        visited<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>linked<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #990000">-</span><span style="color: #993399">1</span> <span style="color: #990000">||</span> <span style="font-weight: bold"><span style="color: #000000">hungarian_dfs</span></span><span style="color: #990000">(</span>linked<span style="color: #990000">[</span>v<span style="color: #990000">]))</span> <span style="color: #FF0000">{</span>
                                linked<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span> linked<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> u<span style="color: #990000">;</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">max_match</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> n <span style="color: #990000">=</span> graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>linked<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> linked<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
                <span style="color: #008080">size_type</span> bi_match <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">size_type</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i <span style="color: #990000">+=</span> <span style="color: #993399">2</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>visited<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> visited<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">hungarian_dfs</span></span><span style="color: #990000">(</span>i<span style="color: #990000">))</span> bi_match<span style="color: #990000">++;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> bi_match<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
        <span style="color: #008080">graph_type</span> graph<span style="color: #990000">;</span>
        <span style="color: #008080">edge_type</span> linked<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;bool&gt;</span> visited<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_flow_dinic_cc_code">15. <code>graph/flow/dinic.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;limits&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;queue&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">dinic</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span>size_type<span style="color: #990000">&gt;::</span><span style="color: #008080">iterator</span> edge_iter<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">edge</span> <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> from<span style="color: #990000">,</span> to<span style="color: #990000">;</span>
                <span style="color: #008080">value_type</span> capacity<span style="color: #990000">,</span> flow<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #000000">dinic</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> num<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">(</span>num<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>num<span style="color: #990000">);</span>      <span style="font-style: italic"><span style="color: #9A1900">//[0, num)</span></span>
                dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>num<span style="color: #990000">);</span>       <span style="font-style: italic"><span style="color: #9A1900">//[0, num)</span></span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> cap <span style="color: #990000">=</span> capacity_inf<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">edge</span> tmp<span style="color: #990000">;</span>
                tmp<span style="color: #990000">.</span>from <span style="color: #990000">=</span> u<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>to <span style="color: #990000">=</span> v<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>capacity <span style="color: #990000">=</span> cap<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>flow <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                edges<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
                graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>edges<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>

                tmp<span style="color: #990000">.</span>from <span style="color: #990000">=</span> v<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>to <span style="color: #990000">=</span> u<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>capacity <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>flow <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                edges<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
                graph<span style="color: #990000">[</span>v<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>edges<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">bfs_label</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> source<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> target<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="color: #008080">queue&lt;size_type&gt;</span> q<span style="color: #990000">;</span>
                q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push</span></span><span style="color: #990000">(</span>source<span style="color: #990000">);</span>
                dist<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(!</span>q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">())</span> <span style="color: #FF0000">{</span>
                        <span style="color: #008080">size_type</span> now <span style="color: #990000">=</span> q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">();</span>
                        q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">pop</span></span><span style="color: #990000">();</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">edge_iter</span> it <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>now<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> it <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>now<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>it<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="color: #008080">edge</span> e <span style="color: #990000">=</span> edges<span style="color: #990000">[*</span>it<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[</span>e<span style="color: #990000">.</span>to<span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #990000">-</span><span style="color: #993399">1</span> <span style="color: #990000">&amp;&amp;</span> e<span style="color: #990000">.</span>capacity <span style="color: #990000">&gt;</span> e<span style="color: #990000">.</span>flow<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                        q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push</span></span><span style="color: #990000">(</span>e<span style="color: #990000">.</span>to<span style="color: #990000">);</span>
                                        dist<span style="color: #990000">[</span>e<span style="color: #990000">.</span>to<span style="color: #990000">]</span> <span style="color: #990000">=</span> dist<span style="color: #990000">[</span>now<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                                <span style="color: #FF0000">}</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> dist<span style="color: #990000">[</span>target<span style="color: #990000">]</span> <span style="color: #990000">!=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">dfs</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> v<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> target<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> f<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>v <span style="color: #990000">==</span> target <span style="color: #990000">||</span> <span style="color: #990000">!</span>f<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> f<span style="color: #990000">;</span>
                <span style="color: #008080">value_type</span> block_flow <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">edge_iter</span> it <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>v<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> it <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>v<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>it<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        edge <span style="color: #990000">&amp;</span> e <span style="color: #990000">=</span> edges<span style="color: #990000">[*</span>it<span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>e<span style="color: #990000">.</span>capacity <span style="color: #990000">&gt;</span> e<span style="color: #990000">.</span>flow <span style="color: #990000">&amp;&amp;</span> dist<span style="color: #990000">[</span>e<span style="color: #990000">.</span>to<span style="color: #990000">]</span> <span style="color: #990000">==</span> dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="color: #008080">value_type</span> tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">dfs</span></span><span style="color: #990000">(</span>e<span style="color: #990000">.</span>to<span style="color: #990000">,</span> target<span style="color: #990000">,</span>
                                                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>e<span style="color: #990000">.</span>capacity <span style="color: #990000">-</span> e<span style="color: #990000">.</span>flow<span style="color: #990000">,</span> f <span style="color: #990000">-</span> block_flow<span style="color: #990000">));</span>
                                block_flow <span style="color: #990000">+=</span> tmp<span style="color: #990000">;</span>
                                e<span style="color: #990000">.</span>flow <span style="color: #990000">+=</span> tmp<span style="color: #990000">;</span>
                                edges<span style="color: #990000">[(*</span>it<span style="color: #990000">)</span> <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">].</span>flow <span style="color: #990000">-=</span> tmp<span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>block_flow<span style="color: #990000">)</span> dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> block_flow<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">max_flow</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> source<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> target<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">value_type</span> flow <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> tmp<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #000000">bfs_label</span></span><span style="color: #990000">(</span>source<span style="color: #990000">,</span> target<span style="color: #990000">);</span> <span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">((</span>tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">dfs</span></span><span style="color: #990000">(</span>source<span style="color: #990000">,</span> target<span style="color: #990000">,</span> capacity_inf<span style="color: #990000">)))</span> flow <span style="color: #990000">+=</span> tmp<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> flow<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

<span style="font-style: italic"><span style="color: #9A1900">//private:</span></span>
        value_type <span style="font-weight: bold"><span style="color: #0000FF">static</span></span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> capacity_inf <span style="color: #990000">=</span> std<span style="color: #990000">::</span>numeric_limits<span style="color: #990000">&lt;</span>value_type<span style="color: #990000">&gt;::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">();</span>
        <span style="color: #008080">size_type</span> size<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> dist<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;edge&gt;</span> edges<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;size_type&gt;&gt;</span> graph<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&gt;&gt;</span> m<span style="color: #990000">;</span>
        <span style="color: #008080">dinic&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">d</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> c<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> x <span style="color: #990000">&gt;&gt;</span> y <span style="color: #990000">&gt;&gt;</span> c<span style="color: #990000">;</span>
                x<span style="color: #990000">--;</span> y<span style="color: #990000">--;</span>
                d<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> c<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">int</span> max_flow <span style="color: #990000">=</span> d<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">max_flow</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> max_flow <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_flow_stoer_wagner_mincut_cc_code">16. <code>graph/flow/stoer-wagner-mincut.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// poj2914</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：给定一个简单无向带边权图，求最小割。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：套用stoer_wagner minimum cut 算法，没有用任何优化，直接</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       邻接矩阵，也没用什么heap优化。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       大概讲下算法。全局最小割为原图一个最小割局面和</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       合并得到s, t后图的最小割局面的最小值。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       而一个最小割局面是这么进行的，初始A集合就是图中任意一个点，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       然后不断把A集合外与A集合点边权和最大的点加入到集合A，最后</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       两个加进来的分别为s, t，最后求的t到集合A的边权和就是局面的最小割。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       详细参见 http://e-maxx.ru/bookz/files/stoer_wagner_mincut.pdf</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 统计：8469ms, 26min, 1A</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">30</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">505</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> graph<span style="color: #990000">[</span>maxn<span style="color: #990000">][</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">bool</span> merged<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">merge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> s<span style="color: #990000">,</span> <span style="color: #009900">int</span> t<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// merge t to s</span></span>
<span style="color: #FF0000">{</span>
        merged<span style="color: #990000">[</span>t<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>merged<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                        graph<span style="color: #990000">[</span>s<span style="color: #990000">][</span>i<span style="color: #990000">]</span> <span style="color: #990000">+=</span> graph<span style="color: #990000">[</span>t<span style="color: #990000">][</span>i<span style="color: #990000">];</span>
                        graph<span style="color: #990000">[</span>i<span style="color: #990000">][</span>s<span style="color: #990000">]</span> <span style="color: #990000">+=</span> graph<span style="color: #990000">[</span>i<span style="color: #990000">][</span>t<span style="color: #990000">];</span>
                <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">bool</span> vis<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> wet<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span> <span style="font-style: italic"><span style="color: #9A1900">// sigma of weight from v to A, A is the current subset of G</span></span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">min_cut_phase</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> <span style="color: #990000">&amp;</span> s<span style="color: #990000">,</span> <span style="color: #009900">int</span> <span style="color: #990000">&amp;</span> t<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>vis<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>vis<span style="color: #990000">));</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>wet<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>wet<span style="color: #990000">));</span>
        s <span style="color: #990000">=</span> t <span style="color: #990000">=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #009900">int</span> max<span style="color: #990000">,</span> min_cut <span style="color: #990000">=</span> inf<span style="color: #990000">,</span> pos<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                max <span style="color: #990000">=</span> <span style="color: #990000">-</span>inf<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>merged<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">!</span>vis<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">&amp;&amp;</span> wet<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> max<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                pos <span style="color: #990000">=</span> j<span style="color: #990000">;</span>
                                max <span style="color: #990000">=</span> wet<span style="color: #990000">[</span>j<span style="color: #990000">];</span>
                        <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t <span style="color: #990000">==</span> pos<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                s <span style="color: #990000">=</span> t<span style="color: #990000">;</span> t <span style="color: #990000">=</span> pos<span style="color: #990000">;</span>
                min_cut <span style="color: #990000">=</span> max<span style="color: #990000">;</span>
                vis<span style="color: #990000">[</span>pos<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>

                <span style="font-style: italic"><span style="color: #9A1900">// add current active verticle to A</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>merged<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">!</span>vis<span style="color: #990000">[</span>j<span style="color: #990000">])</span> wet<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">+=</span> graph<span style="color: #990000">[</span>pos<span style="color: #990000">][</span>j<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> min_cut<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">stoer_wagner</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>merged<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>merged<span style="color: #990000">));</span>
        <span style="color: #009900">int</span> ret <span style="color: #990000">=</span> inf<span style="color: #990000">,</span> s<span style="color: #990000">,</span> t<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> min_cut <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">min_cut_phase</span></span><span style="color: #990000">(</span>s<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
                ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> min_cut<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>ret<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">merge</span></span><span style="color: #990000">(</span>s<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ret<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>m<span style="color: #990000">)</span> <span style="color: #990000">!=</span> EOF<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>graph<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>graph<span style="color: #990000">));</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> z<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>z<span style="color: #990000">);</span>
                        graph<span style="color: #990000">[</span>x<span style="color: #990000">][</span>y<span style="color: #990000">]</span> <span style="color: #990000">+=</span> z<span style="color: #990000">;</span> graph<span style="color: #990000">[</span>y<span style="color: #990000">][</span>x<span style="color: #990000">]</span> <span style="color: #990000">+=</span> z<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">stoer_wagner</span></span><span style="color: #990000">());</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_hamilton_circle_hamilton_circle_ore_cc_code">17. <code>graph/hamilton-circle/hamilton-circle-Ore.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// poj2438</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意： 求一个满足Dirac定理的简单图的hamilton回路</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：https://en.wikipedia.org/wiki/Ore%27s_theorem</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//               可以运用，这个定理证明的构造方法。Palmer给出了一个algorithm.</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//               复杂度O(n^2)。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;deque&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">407</span><span style="color: #990000">;</span>
<span style="color: #009900">bool</span> map<span style="color: #990000">[</span>maxn<span style="color: #990000">][</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">deque&lt;int&gt;</span> data_type<span style="color: #990000">;</span>
<span style="color: #008080">data_type</span> hamilton_circle<span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">reverse</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">,</span> data_type <span style="color: #990000">&amp;</span> a<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// l, r both closed. reverse in a circle.</span></span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> n <span style="color: #990000">=</span> a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
        <span style="color: #009900">int</span> len <span style="color: #990000">=</span> <span style="color: #990000">(</span>r <span style="color: #990000">+</span> n <span style="color: #990000">-</span> l<span style="color: #990000">)</span> <span style="color: #990000">%</span> n <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> count <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> i <span style="color: #990000">=</span> l<span style="color: #990000">;</span> count <span style="color: #990000">&lt;</span> len<span style="color: #990000">/</span><span style="color: #993399">2</span><span style="color: #990000">;</span> count<span style="color: #990000">++,</span> i <span style="color: #990000">=</span> <span style="color: #990000">(</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> n<span style="color: #990000">)</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>a<span style="color: #990000">[</span>i<span style="color: #990000">],</span> a<span style="color: #990000">[(</span>r <span style="color: #990000">+</span> n <span style="color: #990000">-</span> count<span style="color: #990000">)</span> <span style="color: #990000">%</span> n<span style="color: #990000">]);</span>
<span style="color: #FF0000">}</span>

<span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900">void reverse(int l, int r, data_type &amp; a) // l, r both closed. reverse in a circle.</span></span>
<span style="font-style: italic"><span style="color: #9A1900">{</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        int n = a.size();</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        if (l &gt; r) {</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                int len = n - (l - r - 1);</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                std::rotate(a.begin(), a.begin() + l, a.end());</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                std::reverse(a.begin(), a.begin() + len);</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                std::rotate(a.rbegin(), a.rbegin() + l, a.rend());</span></span>
<span style="font-style: italic"><span style="color: #9A1900">        } else</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                std::reverse(a.begin() + l, a.begin() + r + 1);</span></span>
<span style="font-style: italic"><span style="color: #9A1900">}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">*/</span></span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span>ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&gt;&gt;</span> m <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">(</span>n <span style="color: #990000">||</span> m<span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                n <span style="color: #990000">*=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> map<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> x <span style="color: #990000">&gt;&gt;</span> y<span style="color: #990000">;</span>
                        map<span style="color: #990000">[</span>x<span style="color: #990000">][</span>y<span style="color: #990000">]</span> <span style="color: #990000">=</span> map<span style="color: #990000">[</span>y<span style="color: #990000">][</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>

                hamilton_circle<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> hamilton_circle<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">;</span>

                <span style="font-style: italic"><span style="color: #9A1900">// Palmer algorithm meeting Ore's condition</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> time <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> i<span style="color: #990000">,</span> u<span style="color: #990000">,</span> v<span style="color: #990000">;</span> time <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> time<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                                u <span style="color: #990000">=</span> hamilton_circle<span style="color: #990000">[</span>i<span style="color: #990000">];</span> v <span style="color: #990000">=</span> hamilton_circle<span style="color: #990000">[(</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> n<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>map<span style="color: #990000">[</span>u<span style="color: #990000">][</span>v<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>i <span style="color: #990000">==</span> n<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> i <span style="color: #990000">+</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> j <span style="color: #990000">!=</span> <span style="color: #990000">(</span>i<span style="color: #990000">-</span><span style="color: #993399">1</span> <span style="color: #990000">+</span> n<span style="color: #990000">)</span> <span style="color: #990000">%</span> n<span style="color: #990000">;</span> j <span style="color: #990000">=</span> <span style="color: #990000">(</span>j <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> n<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="color: #009900">int</span> u1 <span style="color: #990000">=</span> hamilton_circle<span style="color: #990000">[</span>j <span style="color: #990000">%</span> n<span style="color: #990000">],</span> v1 <span style="color: #990000">=</span> hamilton_circle<span style="color: #990000">[(</span>j<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> n<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!(</span>map<span style="color: #990000">[</span>u<span style="color: #990000">][</span>u1<span style="color: #990000">]</span> <span style="color: #990000">&amp;&amp;</span> map<span style="color: #990000">[</span>v1<span style="color: #990000">][</span>v<span style="color: #990000">]))</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                                <span style="font-weight: bold"><span style="color: #000000">reverse</span></span><span style="color: #990000">((</span>i<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> n<span style="color: #990000">,</span> j <span style="color: #990000">%</span> n<span style="color: #990000">,</span> hamilton_circle<span style="color: #990000">);</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>

                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> hamilton_circle<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span> <span style="color: #990000">&lt;&lt;</span> hamilton_circle<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_kuhn_munkres_v0_cc_code">18. <code>graph/kuhn-munkres_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">//hdu2255</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">using</span></span> <span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> std<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #009900">int</span> maxn <span style="color: #990000">=</span> <span style="color: #993399">301</span><span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #009900">int</span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span><span style="color: #993399">25</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> map<span style="color: #990000">[</span>maxn<span style="color: #990000">][</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> slack<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> linked<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> label_x<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> label_y<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">bool</span> visited_x<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> visited_y<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> nx<span style="color: #990000">,</span> ny<span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init_label</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>label_x<span style="color: #990000">,</span> label_x <span style="color: #990000">+</span> nx<span style="color: #990000">,</span> <span style="color: #990000">-</span>inf<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>label_y<span style="color: #990000">,</span> label_y <span style="color: #990000">+</span> ny<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> nx<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> ny<span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>map<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> label_x<span style="color: #990000">[</span>i<span style="color: #990000">])</span> label_x<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> map<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">hungarian_dfs</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        visited_x<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> ny<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>visited_y<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #009900">int</span> tmp <span style="color: #990000">=</span> label_x<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> label_y<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">-</span> map<span style="color: #990000">[</span>u<span style="color: #990000">][</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tmp <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        visited_y<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>linked<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #990000">-</span><span style="color: #993399">1</span> <span style="color: #990000">||</span> <span style="font-weight: bold"><span style="color: #000000">hungarian_dfs</span></span><span style="color: #990000">(</span>linked<span style="color: #990000">[</span>i<span style="color: #990000">]))</span> <span style="color: #FF0000">{</span>
                                linked<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> u<span style="color: #990000">;</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>slack<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> tmp<span style="color: #990000">)</span> slack<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> tmp<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">kuhn_munkres</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #000000">init_label</span></span><span style="color: #990000">();</span>
        <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>linked<span style="color: #990000">,</span> linked <span style="color: #990000">+</span> ny<span style="color: #990000">,</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> nx<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>slack<span style="color: #990000">,</span> slack <span style="color: #990000">+</span> ny<span style="color: #990000">,</span> inf<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>visited_x<span style="color: #990000">,</span> visited_x <span style="color: #990000">+</span> nx<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>visited_y<span style="color: #990000">,</span> visited_y <span style="color: #990000">+</span> ny<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">hungarian_dfs</span></span><span style="color: #990000">(</span>i<span style="color: #990000">))</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                        <span style="color: #009900">int</span> d <span style="color: #990000">=</span> inf<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> ny<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>visited_y<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&amp;&amp;</span> d <span style="color: #990000">&gt;</span> slack<span style="color: #990000">[</span>i<span style="color: #990000">])</span> d <span style="color: #990000">=</span> slack<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> nx<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>visited_x<span style="color: #990000">[</span>i<span style="color: #990000">])</span> label_x<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">-=</span> d<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> ny<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>visited_y<span style="color: #990000">[</span>i<span style="color: #990000">])</span> label_y<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+=</span> d<span style="color: #990000">;</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>                            slack<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">-=</span> d<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="color: #009900">int</span> max_value <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> ny<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>linked<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> max_value <span style="color: #990000">+=</span> map<span style="color: #990000">[</span>linked<span style="color: #990000">[</span>i<span style="color: #990000">]][</span>i<span style="color: #990000">];</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> max_value<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>cin <span style="color: #990000">&gt;&gt;</span> nx<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                ny <span style="color: #990000">=</span> nx<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> nx<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> ny<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> cin <span style="color: #990000">&gt;&gt;</span> map<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">];</span>

                cout <span style="color: #990000">&lt;&lt;</span> <span style="font-weight: bold"><span style="color: #000000">kuhn_munkres</span></span><span style="color: #990000">()</span> <span style="color: #990000">&lt;&lt;</span> endl<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_lca_tarjan_offline_lca_v0_cc_code">19. <code>graph/lca/tarjan_offline_lca_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// poj1986</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：求树上两点距离，可以离线</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：用离线的tarjan lca做。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;utility&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">node</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> parent<span style="color: #990000">,</span> rank<span style="color: #990000">,</span> ancestor<span style="color: #990000">,</span> color<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">40007</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> cost_to_root<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #008080">node</span> every<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">bool</span> vis<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">,</span> k<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> index<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">pair&lt;int, index&gt;</span> node_type<span style="color: #990000">;</span>

std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::pair&lt;std::pair&lt;int, int&gt;, int&gt; &gt;</span> <span style="font-weight: bold"><span style="color: #000000">input_pair</span></span><span style="color: #990000">(</span>maxn<span style="color: #990000">);</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;node_type&gt; &gt;</span> common_pair<span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;std::pair&lt;int, int&gt; &gt; &gt;</span> graph<span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">make_set</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        every<span style="color: #990000">[</span>u<span style="color: #990000">].</span>parent <span style="color: #990000">=</span> u<span style="color: #990000">;</span>
        every<span style="color: #990000">[</span>u<span style="color: #990000">].</span>rank <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>every<span style="color: #990000">[</span>u<span style="color: #990000">].</span>parent <span style="color: #990000">==</span> u<span style="color: #990000">)</span> <span style="color: #990000">?</span> u <span style="color: #990000">:</span> <span style="color: #990000">(</span>every<span style="color: #990000">[</span>u<span style="color: #990000">].</span>parent <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>every<span style="color: #990000">[</span>u<span style="color: #990000">].</span>parent<span style="color: #990000">));</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">set_union</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> uroot <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>u<span style="color: #990000">);</span>
        <span style="color: #009900">int</span> vroot <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>every<span style="color: #990000">[</span>uroot<span style="color: #990000">].</span>rank <span style="color: #990000">&gt;</span> every<span style="color: #990000">[</span>vroot<span style="color: #990000">].</span>rank<span style="color: #990000">)</span>
                every<span style="color: #990000">[</span>uroot<span style="color: #990000">].</span>parent <span style="color: #990000">=</span> vroot<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>uroot <span style="color: #990000">!=</span> vroot<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                every<span style="color: #990000">[</span>vroot<span style="color: #990000">].</span>parent <span style="color: #990000">=</span> uroot<span style="color: #990000">;</span>
                every<span style="color: #990000">[</span>uroot<span style="color: #990000">].</span>rank<span style="color: #990000">++;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">tarjan_offline_lca</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        vis<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">make_set</span></span><span style="color: #990000">(</span>u<span style="color: #990000">);</span>
        every<span style="color: #990000">[</span>u<span style="color: #990000">].</span>ancestor <span style="color: #990000">=</span> u<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #990000">(</span><span style="color: #009900">int</span><span style="color: #990000">)</span>graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> v <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">][</span>i<span style="color: #990000">].</span>first<span style="color: #990000">,</span> c <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">][</span>i<span style="color: #990000">].</span>second<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>vis<span style="color: #990000">[</span>v<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                cost_to_root<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> cost_to_root<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> c<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">tarjan_offline_lca</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">set_union</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
                every<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>u<span style="color: #990000">)].</span>ancestor <span style="color: #990000">=</span> u<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
        every<span style="color: #990000">[</span>u<span style="color: #990000">].</span>color <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #990000">(</span><span style="color: #009900">int</span><span style="color: #990000">)</span>common_pair<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> v <span style="color: #990000">=</span> common_pair<span style="color: #990000">[</span>u<span style="color: #990000">][</span>i<span style="color: #990000">].</span>first<span style="color: #990000">,</span> index <span style="color: #990000">=</span> common_pair<span style="color: #990000">[</span>u<span style="color: #990000">][</span>i<span style="color: #990000">].</span>second<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>every<span style="color: #990000">[</span>v<span style="color: #990000">].</span>color<span style="color: #990000">)</span>
                        input_pair<span style="color: #990000">[</span>index<span style="color: #990000">].</span>second <span style="color: #990000">=</span> every<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>v<span style="color: #990000">)].</span>ancestor<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>m<span style="color: #990000">);</span>
        graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        common_pair<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="color: #008080">pair&lt;int, int&gt;</span> tmp<span style="color: #990000">;</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d%d%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>tmp<span style="color: #990000">.</span>second<span style="color: #990000">);</span>
                <span style="color: #009900">char</span> ch<span style="color: #990000">;</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">" %c"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>ch<span style="color: #990000">);</span>
                tmp<span style="color: #990000">.</span>first <span style="color: #990000">=</span> y<span style="color: #990000">;</span> graph<span style="color: #990000">[</span>x<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
                tmp<span style="color: #990000">.</span>first <span style="color: #990000">=</span> x<span style="color: #990000">;</span> graph<span style="color: #990000">[</span>y<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>k<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> k<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">);</span>
                input_pair<span style="color: #990000">[</span>i<span style="color: #990000">].</span>first<span style="color: #990000">.</span>first <span style="color: #990000">=</span> x<span style="color: #990000">;</span>
                input_pair<span style="color: #990000">[</span>i<span style="color: #990000">].</span>first<span style="color: #990000">.</span>second <span style="color: #990000">=</span> y<span style="color: #990000">;</span>
                <span style="color: #008080">node_type</span> tmp<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>second <span style="color: #990000">=</span> i<span style="color: #990000">;</span>
                tmp<span style="color: #990000">.</span>first <span style="color: #990000">=</span> y<span style="color: #990000">;</span> common_pair<span style="color: #990000">[</span>x<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
                tmp<span style="color: #990000">.</span>first <span style="color: #990000">=</span> x<span style="color: #990000">;</span> common_pair<span style="color: #990000">[</span>y<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="font-weight: bold"><span style="color: #000000">tarjan_offline_lca</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">);</span>

        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> k<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> u <span style="color: #990000">=</span> input_pair<span style="color: #990000">[</span>i<span style="color: #990000">].</span>first<span style="color: #990000">.</span>first<span style="color: #990000">,</span> v <span style="color: #990000">=</span> input_pair<span style="color: #990000">[</span>i<span style="color: #990000">].</span>first<span style="color: #990000">.</span>second<span style="color: #990000">,</span> c <span style="color: #990000">=</span> input_pair<span style="color: #990000">[</span>i<span style="color: #990000">].</span>second<span style="color: #990000">;</span>
                <span style="color: #009900">int</span> ans <span style="color: #990000">=</span> cost_to_root<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> cost_to_root<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">2</span> <span style="color: #990000">*</span> cost_to_root<span style="color: #990000">[</span>c<span style="color: #990000">];</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> ans<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_shortest_path_dijkstra_v0_cc_code">20. <code>graph/shortest-path/dijkstra_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">//hdu2544</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;queue&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">using</span></span> <span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> std<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #009900">int</span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span><span style="color: #993399">25</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">node</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> to<span style="color: #990000">;</span>
        <span style="color: #009900">int</span> cost<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>vector<span style="color: #990000">&lt;</span>vector<span style="color: #990000">&lt;</span>node<span style="color: #990000">&gt;</span> <span style="color: #990000">&gt;</span> <span style="color: #990000">&amp;</span> graph<span style="color: #990000">,</span> <span style="color: #009900">int</span> x<span style="color: #990000">,</span> <span style="color: #009900">int</span> y<span style="color: #990000">,</span> <span style="color: #009900">int</span> c<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        graph<span style="color: #990000">[</span>x<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span><span style="color: #FF0000">{</span>y<span style="color: #990000">,</span> c<span style="color: #FF0000">}</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">dijkstra</span></span><span style="color: #990000">(</span>vector<span style="color: #990000">&lt;</span>vector<span style="color: #990000">&lt;</span>node<span style="color: #990000">&gt;</span> <span style="color: #990000">&gt;</span> <span style="color: #990000">&amp;</span> graph<span style="color: #990000">,</span> <span style="color: #009900">int</span> source<span style="color: #990000">,</span> <span style="color: #009900">int</span> target<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> v <span style="color: #990000">=</span> graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
        <span style="color: #009900">int</span> dist<span style="color: #990000">[</span>v<span style="color: #990000">];</span>
<span style="font-style: italic"><span style="color: #9A1900">//      bool visited[v];</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//      fill(visited, visited+v, false);</span></span>
        <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>dist<span style="color: #990000">,</span> dist<span style="color: #990000">+</span>v<span style="color: #990000">,</span> inf<span style="color: #990000">);</span>
        dist<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">pair&lt;int, int&gt;</span> end_cost<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">cmp</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #0000FF">operator</span></span><span style="color: #990000">()(</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> end_cost <span style="color: #990000">&amp;</span> a<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> end_cost <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> a<span style="color: #990000">.</span>second <span style="color: #990000">&gt;</span> b<span style="color: #990000">.</span>second<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span><span style="color: #990000">;</span>
        <span style="color: #008080">priority_queue&lt;end_cost, vector&lt;end_cost&gt;, cmp&gt;</span> pq<span style="color: #990000">;</span>
        pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">make_pair</span></span><span style="color: #990000">(</span>source<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">));</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(!</span>pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">())</span> <span style="color: #FF0000">{</span>
                <span style="color: #008080">end_cost</span> tmp <span style="color: #990000">=</span> pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">top</span></span><span style="color: #990000">();</span>
                pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">pop</span></span><span style="color: #990000">();</span>
                <span style="color: #009900">int</span> u <span style="color: #990000">=</span> tmp<span style="color: #990000">.</span>first<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> tmp<span style="color: #990000">.</span>second<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
        <span style="font-style: italic"><span style="color: #9A1900">//      if (visited[u]) continue;</span></span>
        <span style="font-style: italic"><span style="color: #9A1900">//      visited[u] = true;</span></span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>vector<span style="color: #990000">&lt;</span>node<span style="color: #990000">&gt;::</span><span style="color: #008080">iterator</span> edge <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span>
                                edge <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> edge<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[(*</span>edge<span style="color: #990000">).</span>to<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> dist<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #990000">(*</span>edge<span style="color: #990000">).</span>cost<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                dist<span style="color: #990000">[(*</span>edge<span style="color: #990000">).</span>to<span style="color: #990000">]</span> <span style="color: #990000">=</span> dist<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #990000">(*</span>edge<span style="color: #990000">).</span>cost<span style="color: #990000">;</span>
                                pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">make_pair</span></span><span style="color: #990000">((*</span>edge<span style="color: #990000">).</span>to<span style="color: #990000">,</span> dist<span style="color: #990000">[(*</span>edge<span style="color: #990000">).</span>to<span style="color: #990000">]));</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> dist<span style="color: #990000">[</span>target<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&gt;&gt;</span> m <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">(</span>n <span style="color: #990000">!=</span> <span style="color: #993399">0</span> <span style="color: #990000">&amp;&amp;</span> m <span style="color: #990000">!=</span> <span style="color: #993399">0</span><span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                <span style="color: #008080">vector&lt;vector&lt;node&gt; &gt;</span> graph<span style="color: #990000">;</span>
                graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> cost<span style="color: #990000">;</span>
                        cin <span style="color: #990000">&gt;&gt;</span> x <span style="color: #990000">&gt;&gt;</span> y <span style="color: #990000">&gt;&gt;</span> cost<span style="color: #990000">;</span>
                        x<span style="color: #990000">--;</span> y<span style="color: #990000">--;</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>graph<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> cost<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>graph<span style="color: #990000">,</span> y<span style="color: #990000">,</span> x<span style="color: #990000">,</span> cost<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
                cout <span style="color: #990000">&lt;&lt;</span> <span style="font-weight: bold"><span style="color: #000000">dijkstra</span></span><span style="color: #990000">(</span>graph<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> endl<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_shortest_path_dijkstra_v1_cc_code">21. <code>graph/shortest-path/dijkstra_v1.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// bzoj2662</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：给定一个边上费用为偶数的无向联通图，可以选择路径上最多k条边使得时间</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       费用减半，问从1到n最少费用。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：由于n比较小，就构建一个k层的分层图跑最短路就好。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       dijkstra模板。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;queue&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;utility&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;functional&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">node</span> <span style="color: #FF0000">{</span> <span style="color: #009900">int</span> to<span style="color: #990000">,</span> cost<span style="color: #990000">;</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">29</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">3000</span><span style="color: #990000">;</span>

std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;node&gt;</span> graph<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span>node<span style="color: #990000">&gt;::</span><span style="color: #008080">iterator</span> edge_iter<span style="color: #990000">;</span>

<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">,</span> k<span style="color: #990000">,</span> s<span style="color: #990000">,</span> t<span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">index_kth</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">,</span> <span style="color: #009900">int</span> k<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> x <span style="color: #990000">+</span> k <span style="color: #990000">*</span> n<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">,</span> <span style="color: #009900">int</span> y<span style="color: #990000">,</span> <span style="color: #009900">int</span> z<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        graph<span style="color: #990000">[</span>x<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span><span style="color: #FF0000">{</span>y<span style="color: #990000">,</span> z<span style="color: #FF0000">}</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">dijkstra</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> source<span style="color: #990000">,</span> <span style="color: #009900">int</span> target<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> num <span style="color: #990000">=</span> t <span style="color: #990000">+</span> <span style="color: #993399">4</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// tot vertices</span></span>
        <span style="color: #009900">int</span> dist<span style="color: #990000">[</span>num<span style="color: #990000">];</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>dist<span style="color: #990000">,</span> dist <span style="color: #990000">+</span> num<span style="color: #990000">,</span> inf<span style="color: #990000">);</span>
        dist<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">pair&lt;int, int&gt;</span> cost_end<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">priority_queue&lt;cost_end, std::vector&lt;cost_end&gt;, std::greater&lt;cost_end&gt; &gt;</span> pq<span style="color: #990000">;</span>
        pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">make_pair</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> source<span style="color: #990000">));</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(!</span>pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">())</span> <span style="color: #FF0000">{</span>
                <span style="color: #008080">cost_end</span> tmp <span style="color: #990000">=</span> pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">top</span></span><span style="color: #990000">();</span> pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">pop</span></span><span style="color: #990000">();</span>
                <span style="color: #009900">int</span> u <span style="color: #990000">=</span> tmp<span style="color: #990000">.</span>second<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> tmp<span style="color: #990000">.</span>first<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">edge_iter</span> edge <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> edge <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>edge<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> v <span style="color: #990000">=</span> edge<span style="color: #990000">-&gt;</span>to<span style="color: #990000">,</span> c <span style="color: #990000">=</span> edge<span style="color: #990000">-&gt;</span>cost<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> dist<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> c<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> dist<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> c<span style="color: #990000">;</span>
                                pq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">make_pair</span></span><span style="color: #990000">(</span>dist<span style="color: #990000">[</span>v<span style="color: #990000">],</span> v<span style="color: #990000">));</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> dist<span style="color: #990000">[</span>target<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&gt;&gt;</span> m <span style="color: #990000">&gt;&gt;</span> k<span style="color: #990000">;</span>
        s <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">index_kth</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> k<span style="color: #990000">);</span>
        t <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">index_kth</span></span><span style="color: #990000">(</span>n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">,</span> k<span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> z<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> x <span style="color: #990000">&gt;&gt;</span> y <span style="color: #990000">&gt;&gt;</span> z<span style="color: #990000">;</span>
                x<span style="color: #990000">--;</span> y<span style="color: #990000">--;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;=</span> k<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> u <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">index_kth</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> j<span style="color: #990000">);</span>
                        <span style="color: #009900">int</span> v <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">index_kth</span></span><span style="color: #990000">(</span>y<span style="color: #990000">,</span> j<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">,</span> z<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>v<span style="color: #990000">,</span> u<span style="color: #990000">,</span> z<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>j <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">index_kth</span></span><span style="color: #990000">(</span>y<span style="color: #990000">,</span> j<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">),</span> z<span style="color: #990000">/</span><span style="color: #993399">2</span><span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>v<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">index_kth</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> j<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">),</span> z<span style="color: #990000">/</span><span style="color: #993399">2</span><span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> k<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">index_kth</span></span><span style="color: #990000">(</span>n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">,</span> i<span style="color: #990000">),</span> t<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
        <span style="color: #009900">int</span> ans <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">dijkstra</span></span><span style="color: #990000">(</span>s<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> ans <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_shortest_path_spfa_v0_cc_code">22. <code>graph/shortest-path/spfa_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">//poj2387</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;deque&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">using</span></span> <span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> std<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #009900">int</span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span><span style="color: #993399">25</span><span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #009900">int</span> maxn <span style="color: #990000">=</span> <span style="color: #993399">1010</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> map<span style="color: #990000">[</span>maxn<span style="color: #990000">][</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> dist<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">bool</span> visited<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">spfa</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> source<span style="color: #990000">,</span> <span style="color: #009900">int</span> target<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>dist<span style="color: #990000">,</span> dist<span style="color: #990000">+</span>n<span style="color: #990000">,</span> inf<span style="color: #990000">);</span>
        <span style="color: #008080">deque&lt;int&gt;</span> relaxed<span style="color: #990000">;</span>
        relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>source<span style="color: #990000">);</span>
        dist<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        visited<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-style: italic"><span style="color: #9A1900">//int sum = 0, tot = 1;</span></span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(!</span>relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">())</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> x <span style="color: #990000">=</span> relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">();</span>
                relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">pop_front</span></span><span style="color: #990000">();</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>map<span style="color: #990000">[</span>x<span style="color: #990000">][</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span> <span style="color: #990000">&amp;&amp;</span> dist<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> dist<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">+</span> map<span style="color: #990000">[</span>x<span style="color: #990000">][</span>i<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                                dist<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> map<span style="color: #990000">[</span>x<span style="color: #990000">][</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> dist<span style="color: #990000">[</span>x<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>visited<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                                        <span style="font-style: italic"><span style="color: #9A1900">//small label first(slf)</span></span>
                                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">()</span> <span style="color: #990000">&amp;&amp;</span> dist<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> dist<span style="color: #990000">[</span>relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">()])</span>
                                                relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_front</span></span><span style="color: #990000">(</span>i<span style="color: #990000">);</span>
                                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                                relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>i<span style="color: #990000">);</span>
                                        <span style="font-style: italic"><span style="color: #9A1900">//sum += dist[i]; tot++;</span></span>
                                        visited<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                                <span style="color: #FF0000">}</span>
                        <span style="color: #FF0000">}</span>
                visited<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
                <span style="font-style: italic"><span style="color: #9A1900">//large label last optimization not work well</span></span>
                <span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                tot--; sum -= dist[x];</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                if (!relaxed.empty()) {</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                        int avg = sum/tot;</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                        while (dist[relaxed.front()] &gt; avg) {</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                                int u = relaxed.front();</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                                relaxed.pop_front();</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                                relaxed.push_back(u);</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                        }</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                }</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                */</span></span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> dist<span style="color: #990000">[</span>target<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        cin <span style="color: #990000">&gt;&gt;</span> m <span style="color: #990000">&gt;&gt;</span> n<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(&amp;</span>map<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">][</span><span style="color: #993399">0</span><span style="color: #990000">],</span> <span style="color: #990000">&amp;</span>map<span style="color: #990000">[</span>n<span style="color: #990000">][</span>n<span style="color: #990000">],</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> cost<span style="color: #990000">;</span>
                cin <span style="color: #990000">&gt;&gt;</span> x <span style="color: #990000">&gt;&gt;</span> y <span style="color: #990000">&gt;&gt;</span> cost<span style="color: #990000">;</span>
                x<span style="color: #990000">--;</span> y<span style="color: #990000">--;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!(</span>map<span style="color: #990000">[</span>x<span style="color: #990000">][</span>y<span style="color: #990000">]</span> <span style="color: #990000">!=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span> <span style="color: #990000">&amp;&amp;</span> map<span style="color: #990000">[</span>x<span style="color: #990000">][</span>y<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> cost<span style="color: #990000">))</span>
                        map<span style="color: #990000">[</span>x<span style="color: #990000">][</span>y<span style="color: #990000">]</span> <span style="color: #990000">=</span> map<span style="color: #990000">[</span>y<span style="color: #990000">][</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> cost<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        cout <span style="color: #990000">&lt;&lt;</span> <span style="font-weight: bold"><span style="color: #000000">spfa</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> endl<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_shortest_path_spfa_v1_1_cc_code">23. <code>graph/shortest-path/spfa_v1_1.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// poj1201</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// rewrite deque, fuck poj.</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: time $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">shortest_path_faster_algorithm</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">edge</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">edge</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> from<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> to<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> cost<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">from</span></span><span style="color: #990000">(</span>from<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">to</span></span><span style="color: #990000">(</span>to<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">cost</span></span><span style="color: #990000">(</span>cost<span style="color: #990000">)</span> <span style="color: #FF0000">{}</span>
                <span style="color: #008080">size_type</span> from<span style="color: #990000">,</span> to<span style="color: #990000">;</span>
                <span style="color: #008080">value_type</span> cost<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;edge&gt;</span> adj_edge<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="font-weight: bold"><span style="color: #0000FF">typename</span></span> <span style="color: #008080">adj_edge</span><span style="color: #990000">::</span><span style="color: #008080">iterator</span> edge_iter<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;adj_edge&gt;</span> graph_type<span style="color: #990000">;</span>
<span style="font-style: italic"><span style="color: #9A1900">//      enum { inf = 1 &lt;&lt; 28 };</span></span>
        value_type <span style="font-weight: bold"><span style="color: #0000FF">static</span></span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">28</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #000000">shortest_path_faster_algorithm</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> n<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">n</span></span><span style="color: #990000">(</span>n<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                in_queue<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                count_into_que<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                dq_size <span style="color: #990000">=</span> <span style="color: #993399">2</span> <span style="color: #990000">*</span> n<span style="color: #990000">;</span>
                dq<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>dq_size<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> from<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> to<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> cost<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                graph<span style="color: #990000">[</span>from<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">edge</span></span><span style="color: #990000">(</span>from<span style="color: #990000">,</span> to<span style="color: #990000">,</span> cost<span style="color: #990000">));</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">spfa</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> source<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> target<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="color: #990000">+</span>inf<span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>in_queue<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> in_queue<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>count_into_que<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> count_into_que<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">clear</span></span><span style="color: #990000">();</span>
                <span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>source<span style="color: #990000">);</span>
                dist<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                in_queue<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                count_into_que<span style="color: #990000">[</span>source<span style="color: #990000">]++;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(!</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">())</span> <span style="color: #FF0000">{</span>
                        <span style="color: #008080">size_type</span> now <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">();</span>
                        in_queue<span style="color: #990000">[</span>now<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #000000">pop_front</span></span><span style="color: #990000">();</span>

                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">edge_iter</span> it <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>now<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> it <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>now<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>it<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="color: #008080">size_type</span> v <span style="color: #990000">=</span> it<span style="color: #990000">-&gt;</span>to<span style="color: #990000">;</span>
                                <span style="color: #008080">value_type</span> c <span style="color: #990000">=</span> it<span style="color: #990000">-&gt;</span>cost<span style="color: #990000">;</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> dist<span style="color: #990000">[</span>now<span style="color: #990000">]</span> <span style="color: #990000">+</span> c<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                        dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> dist<span style="color: #990000">[</span>now<span style="color: #990000">]</span> <span style="color: #990000">+</span> c<span style="color: #990000">;</span>
                                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>in_queue<span style="color: #990000">[</span>v<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>count_into_que<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&gt;=</span> n<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">-</span>inf<span style="color: #990000">;</span>
                                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">()</span> <span style="color: #990000">&amp;&amp;</span> dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> dist<span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">()])</span>
                                                        <span style="font-weight: bold"><span style="color: #000000">push_front</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
                                                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                                        <span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
                                                in_queue<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                                                count_into_que<span style="color: #990000">[</span>v<span style="color: #990000">]++;</span>
                                        <span style="color: #FF0000">}</span>
                                <span style="color: #FF0000">}</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> dist<span style="color: #990000">[</span>target<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">next</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> dq_size<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">prev</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">+</span> dq_size <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> dq_size<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">clear</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> head <span style="color: #990000">=</span> tail <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> head <span style="color: #990000">==</span> tail<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #008080">size_type</span> <span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> dq<span style="color: #990000">[</span>head<span style="color: #990000">];</span> <span style="color: #FF0000">}</span>
        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> dq<span style="color: #990000">[</span>tail<span style="color: #990000">]</span> <span style="color: #990000">=</span> x<span style="color: #990000">;</span> tail <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">next</span></span><span style="color: #990000">(</span>tail<span style="color: #990000">);</span> <span style="color: #FF0000">}</span>
        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">push_front</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> x<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> dq<span style="color: #990000">[</span>head <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">prev</span></span><span style="color: #990000">(</span>head<span style="color: #990000">)]</span> <span style="color: #990000">=</span> x<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">pop_front</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> head <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">next</span></span><span style="color: #990000">(</span>head<span style="color: #990000">);</span> <span style="color: #FF0000">}</span>

        <span style="color: #008080">graph_type</span> graph<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> n<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> dq_size<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> head<span style="color: #990000">,</span> tail<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;size_type&gt;</span> dq<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;size_type&gt;</span> count_into_que<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;value_type&gt;</span> dist<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;char&gt;</span> in_queue<span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// FIXME std::vector&lt;bool&gt;;</span></span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">data</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> l<span style="color: #990000">,</span> r<span style="color: #990000">,</span> c<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">50004</span><span style="color: #990000">;</span>
<span style="color: #008080">data</span> da<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> max_r<span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d%d%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>l<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>r<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>c<span style="color: #990000">);</span>
                da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>l<span style="color: #990000">++;</span> da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>r<span style="color: #990000">++;</span>
                max_r <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>max_r<span style="color: #990000">,</span> da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>r<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">shortest_path_faster_algorithm&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">sp</span></span><span style="color: #990000">(</span>max_r <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> max_r<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>i<span style="color: #990000">,</span> i <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>i <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> i<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>l <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>r<span style="color: #990000">,</span> <span style="color: #990000">-</span>da<span style="color: #990000">[</span>i<span style="color: #990000">].</span>c<span style="color: #990000">);</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> <span style="color: #990000">-</span>sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">spfa</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> max_r<span style="color: #990000">));</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_shortest_path_spfa_v1_cc_code">24. <code>graph/shortest-path/spfa_v1.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// poj1275</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：给定一天24个小时每个小时需要的员工数目，然后给定n个应聘的员工</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       以及应聘的时间，每个员工从应聘时间开始工作8小时。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       问在满足每小时员工工作情况的条件下，最少可以有几个员工。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：为避免负数，时间计数1～24。令：</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       R[i] i时间需要的人数 （1 &lt;= i &lt;= 24）</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       T[i] i时间应聘的人数 （1 &lt;= i &lt;= 24）</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       x[i] i时间录用的人数 （0 &lt;= i &lt;= 24），其中令x[0] = 0</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       needed[i] 表示从i-1 ~ i这一个小时需要的员工，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       applied[i] 表示第 i 个小时来应聘的员工个数。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       记s[i] = x[0] + x[1] + ... + x[i] （0 &lt;= i &lt;= 24），</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       那么需要满足以下方程:</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       (1) s[i - 1] - s[i] &lt;= 0                       (1 &lt;= i &lt;= 24)</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       (2) s[i] - s[i - 1] &lt;= applied[i]      (1 &lt;= i &lt;= 24)</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       (3) s[i - 9] - s[i - 1] &lt;= -needed[i] (i &gt;= 9)</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       (4) s[i + 15] - s[i - 1] &lt;= sum - needed[i] (i &lt; 9)</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       (5) s[24] - s[0] &lt;= -sum</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       sum是要二分的答案。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: time $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;deque&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">shortest_path_faster_algorithm</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">edge</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">edge</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> from<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> to<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> cost<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">from</span></span><span style="color: #990000">(</span>from<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">to</span></span><span style="color: #990000">(</span>to<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">cost</span></span><span style="color: #990000">(</span>cost<span style="color: #990000">)</span> <span style="color: #FF0000">{}</span>
                <span style="color: #008080">size_type</span> from<span style="color: #990000">,</span> to<span style="color: #990000">;</span>
                <span style="color: #008080">value_type</span> cost<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;edge&gt;</span> adj_edge<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="font-weight: bold"><span style="color: #0000FF">typename</span></span> <span style="color: #008080">adj_edge</span><span style="color: #990000">::</span><span style="color: #008080">iterator</span> edge_iter<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;adj_edge&gt;</span> graph_type<span style="color: #990000">;</span>
<span style="font-style: italic"><span style="color: #9A1900">//      enum { inf = 1 &lt;&lt; 28 };</span></span>
        value_type <span style="font-weight: bold"><span style="color: #0000FF">static</span></span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">28</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #000000">shortest_path_faster_algorithm</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> n<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">n</span></span><span style="color: #990000">(</span>n<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                in_queue<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                count_into_que<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> from<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> to<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> cost<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                graph<span style="color: #990000">[</span>from<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">edge</span></span><span style="color: #990000">(</span>from<span style="color: #990000">,</span> to<span style="color: #990000">,</span> cost<span style="color: #990000">));</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">spfa</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> source<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> target<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="color: #990000">+</span>inf<span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>in_queue<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> in_queue<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>count_into_que<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> count_into_que<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="color: #008080">deque&lt;value_type&gt;</span> <span style="font-weight: bold"><span style="color: #000000">relaxed</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> source<span style="color: #990000">);</span>
                dist<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                in_queue<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                count_into_que<span style="color: #990000">[</span>source<span style="color: #990000">]++;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(!</span>relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">())</span> <span style="color: #FF0000">{</span>
                        <span style="color: #008080">size_type</span> now <span style="color: #990000">=</span> relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">();</span>
                        in_queue<span style="color: #990000">[</span>now<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
                        relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">pop_front</span></span><span style="color: #990000">();</span>

                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">edge_iter</span> it <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>now<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> it <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>now<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>it<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="color: #008080">size_type</span> v <span style="color: #990000">=</span> it<span style="color: #990000">-&gt;</span>to<span style="color: #990000">;</span>
                                <span style="color: #008080">value_type</span> c <span style="color: #990000">=</span> it<span style="color: #990000">-&gt;</span>cost<span style="color: #990000">;</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> dist<span style="color: #990000">[</span>now<span style="color: #990000">]</span> <span style="color: #990000">+</span> c<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                        dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> dist<span style="color: #990000">[</span>now<span style="color: #990000">]</span> <span style="color: #990000">+</span> c<span style="color: #990000">;</span>
                                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>in_queue<span style="color: #990000">[</span>v<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>count_into_que<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&gt;=</span> n<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">-</span>inf<span style="color: #990000">;</span>
                                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">()</span> <span style="color: #990000">&amp;&amp;</span> dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> dist<span style="color: #990000">[</span>relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">()])</span>
                                                        relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_front</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
                                                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                                        relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
                                                in_queue<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                                                count_into_que<span style="color: #990000">[</span>v<span style="color: #990000">]++;</span>
                                        <span style="color: #FF0000">}</span>
                                <span style="color: #FF0000">}</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> dist<span style="color: #990000">[</span>target<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
        <span style="color: #008080">graph_type</span> graph<span style="color: #990000">;</span>
        <span style="color: #008080">size_type</span> n<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;size_type&gt;</span> count_into_que<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;value_type&gt;</span> dist<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;char&gt;</span> in_queue<span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// FIXME std::vector&lt;bool&gt;;</span></span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">25</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> needed<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> applied<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>

<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">judge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> sum<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> n <span style="color: #990000">=</span> <span style="color: #993399">25</span><span style="color: #990000">;</span>
        <span style="color: #008080">shortest_path_faster_algorithm&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">sp</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>i <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> i<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>i<span style="color: #990000">,</span> i <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> applied<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
        sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #993399">24</span><span style="color: #990000">,</span> <span style="color: #990000">-</span>sum<span style="color: #990000">);</span>

        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>i <span style="color: #990000">&gt;=</span> <span style="color: #993399">9</span><span style="color: #990000">)</span>
                        sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>i <span style="color: #990000">-</span> <span style="color: #993399">9</span><span style="color: #990000">,</span> i <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #990000">-</span>needed<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>i <span style="color: #990000">+</span> <span style="color: #993399">15</span><span style="color: #990000">,</span> i <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> sum <span style="color: #990000">-</span> needed<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> sp<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">spfa</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #993399">24</span><span style="color: #990000">)</span> <span style="color: #990000">!=</span> <span style="color: #990000">-</span>sp<span style="color: #990000">.</span>inf<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span>ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        <span style="color: #009900">int</span> T<span style="color: #990000">;</span> std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> T<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>T<span style="color: #990000">--)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>applied<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>applied<span style="color: #990000">));</span>
                <span style="color: #009900">int</span> n <span style="color: #990000">=</span> <span style="color: #993399">24</span><span style="color: #990000">,</span> m<span style="color: #990000">,</span> sum <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> needed<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> sum <span style="color: #990000">+=</span> needed<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> m<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> h<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> h<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>h<span style="color: #990000">)</span> h <span style="color: #990000">=</span> <span style="color: #993399">24</span><span style="color: #990000">;</span>
                        applied<span style="color: #990000">[</span>h<span style="color: #990000">]++;</span>
                <span style="color: #FF0000">}</span>
                <span style="color: #009900">int</span> l <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> r <span style="color: #990000">=</span> m<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">+</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;=</span> r<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> mid <span style="color: #990000">=</span> <span style="color: #990000">(</span>l <span style="color: #990000">+</span> r<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">judge</span></span><span style="color: #990000">(</span>mid<span style="color: #990000">))</span> r <span style="color: #990000">=</span> mid<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>                    l <span style="color: #990000">=</span> mid <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">judge</span></span><span style="color: #990000">(</span>l<span style="color: #990000">)</span> <span style="color: #990000">||</span> <span style="font-weight: bold"><span style="color: #000000">judge</span></span><span style="color: #990000">(</span>r<span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">judge</span></span><span style="color: #990000">(</span>l<span style="color: #990000">))</span> r <span style="color: #990000">=</span> l<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> r <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"No Solution</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_tmp_cc_code">25. <code>graph/tmp.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;queue&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">edge</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> from<span style="color: #990000">,</span> to<span style="color: #990000">,</span> index<span style="color: #990000">;</span>
        <span style="color: #009900">long</span> <span style="color: #009900">long</span> c<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">long</span> <span style="color: #009900">long</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> big <span style="color: #990000">=</span> <span style="color: #993399">100000</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">2005</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxm <span style="color: #990000">=</span> <span style="color: #993399">60006</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">,</span> min_len<span style="color: #990000">;</span>

std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;edge&gt; &gt;</span> graph<span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;edge&gt; &gt;</span> g2<span style="color: #990000">;</span>

<span style="color: #009900">long</span> <span style="color: #009900">long</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> inf <span style="color: #990000">=</span> 1000000000000000LL<span style="color: #990000">;</span>
<span style="color: #009900">long</span> <span style="color: #009900">long</span> dist<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">bool</span> visited<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>

<span style="color: #009900">long</span> <span style="color: #009900">long</span> <span style="font-weight: bold"><span style="color: #000000">spfa</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> source<span style="color: #990000">,</span> <span style="color: #009900">int</span> target<span style="color: #990000">,</span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;edge&gt; &gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> g<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>dist<span style="color: #990000">,</span> dist<span style="color: #990000">+</span>n<span style="color: #990000">,</span> inf<span style="color: #990000">);</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>visited<span style="color: #990000">,</span> visited<span style="color: #990000">+</span>n<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
        std<span style="color: #990000">::</span><span style="color: #008080">deque&lt;int&gt;</span> relaxed<span style="color: #990000">;</span>
        relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>source<span style="color: #990000">);</span>
        dist<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        visited<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(!</span>relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">())</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> x <span style="color: #990000">=</span> relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">();</span>
                relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">pop_front</span></span><span style="color: #990000">();</span>
                visited<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #990000">(</span><span style="color: #009900">int</span><span style="color: #990000">)</span>g<span style="color: #990000">[</span>x<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> v <span style="color: #990000">=</span> g<span style="color: #990000">[</span>x<span style="color: #990000">][</span>i<span style="color: #990000">].</span>to<span style="color: #990000">,</span> e <span style="color: #990000">=</span> g<span style="color: #990000">[</span>x<span style="color: #990000">][</span>i<span style="color: #990000">].</span>c<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> dist<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">+</span> e<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> e <span style="color: #990000">+</span> dist<span style="color: #990000">[</span>x<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>visited<span style="color: #990000">[</span>v<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">()</span> <span style="color: #990000">&amp;&amp;</span> dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> dist<span style="color: #990000">[</span>relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">()])</span>
                                                relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_front</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
                                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                                relaxed<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>v<span style="color: #990000">);</span>
                                        visited<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                                <span style="color: #FF0000">}</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> dist<span style="color: #990000">[</span>target<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span>


<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> capacity_inf <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span><span style="color: #993399">27</span><span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">dinic</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">T</span> value_type<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">int</span> size_type<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">dinic_edge</span> <span style="color: #FF0000">{</span>
                <span style="color: #008080">size_type</span> from<span style="color: #990000">,</span> to<span style="color: #990000">;</span>
                <span style="color: #008080">value_type</span> capacity<span style="color: #990000">,</span> flow<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #000000">dinic</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> num<span style="color: #990000">)</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">(</span>num<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>num<span style="color: #990000">);</span>    <span style="font-style: italic"><span style="color: #9A1900">//[0, num)</span></span>
                dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>num<span style="color: #990000">);</span>    <span style="font-style: italic"><span style="color: #9A1900">//[0, num)</span></span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> cap<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">dinic_edge</span> tmp<span style="color: #990000">;</span>
                tmp<span style="color: #990000">.</span>from <span style="color: #990000">=</span> u<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>to <span style="color: #990000">=</span> v<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>capacity <span style="color: #990000">=</span> cap<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>flow <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                edges<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
                graph<span style="color: #990000">[</span>u<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>edges<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>

                tmp<span style="color: #990000">.</span>from <span style="color: #990000">=</span> v<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>to <span style="color: #990000">=</span> u<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>capacity <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>flow <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                edges<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
                graph<span style="color: #990000">[</span>v<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>edges<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">bfs_label</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> source<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> target<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> dist<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="color: #008080">queue&lt;size_type&gt;</span> q<span style="color: #990000">;</span>
                q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push</span></span><span style="color: #990000">(</span>source<span style="color: #990000">);</span>
                dist<span style="color: #990000">[</span>source<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(!</span>q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">())</span> <span style="color: #FF0000">{</span>
                        <span style="color: #008080">size_type</span> now <span style="color: #990000">=</span> q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">front</span></span><span style="color: #990000">();</span>
                        q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">pop</span></span><span style="color: #990000">();</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span>size_type<span style="color: #990000">&gt;::</span><span style="color: #008080">iterator</span> it <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>now<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> it <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>now<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>it<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="color: #008080">dinic_edge</span> e <span style="color: #990000">=</span> edges<span style="color: #990000">[*</span>it<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[</span>e<span style="color: #990000">.</span>to<span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #990000">-</span><span style="color: #993399">1</span> <span style="color: #990000">&amp;&amp;</span> e<span style="color: #990000">.</span>capacity <span style="color: #990000">&gt;</span> e<span style="color: #990000">.</span>flow<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                        q<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push</span></span><span style="color: #990000">(</span>e<span style="color: #990000">.</span>to<span style="color: #990000">);</span>
                                        dist<span style="color: #990000">[</span>e<span style="color: #990000">.</span>to<span style="color: #990000">]</span> <span style="color: #990000">=</span> dist<span style="color: #990000">[</span>now<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                                <span style="color: #FF0000">}</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> dist<span style="color: #990000">[</span>target<span style="color: #990000">]</span> <span style="color: #990000">!=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">dfs</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> v<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> target<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> f<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>v <span style="color: #990000">==</span> target <span style="color: #990000">||</span> <span style="color: #990000">!</span>f<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> f<span style="color: #990000">;</span>
                <span style="color: #008080">value_type</span> block_flow <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span>size_type<span style="color: #990000">&gt;::</span><span style="color: #008080">iterator</span> it <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>v<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> it <span style="color: #990000">!=</span> graph<span style="color: #990000">[</span>v<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>it<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        dinic_edge <span style="color: #990000">&amp;</span> e <span style="color: #990000">=</span> edges<span style="color: #990000">[*</span>it<span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>e<span style="color: #990000">.</span>capacity <span style="color: #990000">&gt;</span> e<span style="color: #990000">.</span>flow <span style="color: #990000">&amp;&amp;</span> dist<span style="color: #990000">[</span>e<span style="color: #990000">.</span>to<span style="color: #990000">]</span> <span style="color: #990000">==</span> dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="color: #008080">value_type</span> tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">dfs</span></span><span style="color: #990000">(</span>e<span style="color: #990000">.</span>to<span style="color: #990000">,</span> target<span style="color: #990000">,</span>
                                                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>e<span style="color: #990000">.</span>capacity <span style="color: #990000">-</span> e<span style="color: #990000">.</span>flow<span style="color: #990000">,</span> f <span style="color: #990000">-</span> block_flow<span style="color: #990000">));</span>
                                block_flow <span style="color: #990000">+=</span> tmp<span style="color: #990000">;</span>
                                e<span style="color: #990000">.</span>flow <span style="color: #990000">+=</span> tmp<span style="color: #990000">;</span>
                                edges<span style="color: #990000">[(*</span>it<span style="color: #990000">)</span> <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">].</span>flow <span style="color: #990000">-=</span> tmp<span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>block_flow<span style="color: #990000">)</span> dist<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> block_flow<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">max_flow</span></span><span style="color: #990000">(</span><span style="color: #008080">size_type</span> source<span style="color: #990000">,</span> <span style="color: #008080">size_type</span> target<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">value_type</span> flow <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> tmp<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #000000">bfs_label</span></span><span style="color: #990000">(</span>source<span style="color: #990000">,</span> target<span style="color: #990000">);</span> <span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">((</span>tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">dfs</span></span><span style="color: #990000">(</span>source<span style="color: #990000">,</span> target<span style="color: #990000">,</span> capacity_inf<span style="color: #990000">)))</span> flow <span style="color: #990000">+=</span> tmp<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> flow<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">//private:</span></span>
        <span style="color: #008080">size_type</span> size<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> dist<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;dinic_edge&gt;</span> edges<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;size_type&gt; &gt;</span> graph<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>


<span style="color: #009900">bool</span> vis_edge<span style="color: #990000">[</span>maxm<span style="color: #990000">];</span>
<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init_graph_dfs</span></span><span style="color: #990000">(</span>dinic<span style="color: #990000">&lt;</span><span style="color: #009900">int</span><span style="color: #990000">&gt;</span> <span style="color: #990000">&amp;</span> d<span style="color: #990000">,</span> <span style="color: #009900">int</span> x<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #990000">(</span><span style="color: #009900">int</span><span style="color: #990000">)</span>graph<span style="color: #990000">[</span>x<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #008080">edge</span> t <span style="color: #990000">=</span> graph<span style="color: #990000">[</span>x<span style="color: #990000">][</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>vis_edge<span style="color: #990000">[</span>t<span style="color: #990000">.</span>index<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dist<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">==</span> dist<span style="color: #990000">[</span>t<span style="color: #990000">.</span>to<span style="color: #990000">]</span> <span style="color: #990000">+</span> t<span style="color: #990000">.</span>c<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-style: italic"><span style="color: #9A1900">//    std::cerr &lt;&lt; "----&gt;" &lt;&lt; t.index &lt;&lt; ' ' &lt;&lt; t.from + 1 &lt;&lt; ' ' &lt;&lt; x + 1 &lt;&lt; '\n';</span></span>
                        d<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>t<span style="color: #990000">.</span>to<span style="color: #990000">,</span> x<span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                        vis_edge<span style="color: #990000">[</span>t<span style="color: #990000">.</span>index<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #000000">init_graph_dfs</span></span><span style="color: #990000">(</span>d<span style="color: #990000">,</span> t<span style="color: #990000">.</span>to<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> c<span style="color: #990000">;</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>m<span style="color: #990000">)</span> <span style="color: #990000">!=</span> EOF<span style="color: #990000">;</span> <span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">clear</span></span><span style="color: #990000">();</span> graph<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                g2<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">clear</span></span><span style="color: #990000">();</span> g2<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d%d%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>c<span style="color: #990000">);</span>
                        x<span style="color: #990000">--;</span> y<span style="color: #990000">--;</span>
                        <span style="color: #008080">edge</span> tmp<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>index <span style="color: #990000">=</span> i<span style="color: #990000">;</span>
                        tmp<span style="color: #990000">.</span>to <span style="color: #990000">=</span> y<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>c <span style="color: #990000">=</span> c<span style="color: #990000">;</span>
                        graph<span style="color: #990000">[</span>x<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
                        tmp<span style="color: #990000">.</span>to <span style="color: #990000">=</span> x<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>c <span style="color: #990000">=</span> c<span style="color: #990000">;</span>
                        graph<span style="color: #990000">[</span>y<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>

                        tmp<span style="color: #990000">.</span>to <span style="color: #990000">=</span> y<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>c <span style="color: #990000">=</span> big <span style="color: #990000">*</span> c <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                        g2<span style="color: #990000">[</span>x<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
                        tmp<span style="color: #990000">.</span>to <span style="color: #990000">=</span> x<span style="color: #990000">;</span> tmp<span style="color: #990000">.</span>c <span style="color: #990000">=</span> big <span style="color: #990000">*</span> c <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                        g2<span style="color: #990000">[</span>y<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>

                <span style="color: #009900">int</span> ans1<span style="color: #990000">,</span> ans2<span style="color: #990000">;</span>
                min_len <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">spfa</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">,</span> g2<span style="color: #990000">)</span> <span style="color: #990000">%</span> big<span style="color: #990000">;</span>
                ans2 <span style="color: #990000">=</span> m <span style="color: #990000">-</span> min_len<span style="color: #990000">;</span>

                <span style="font-style: italic"><span style="color: #9A1900">//int min_path =</span></span>
                <span style="font-weight: bold"><span style="color: #000000">spfa</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">,</span> graph<span style="color: #990000">);</span>

                <span style="font-style: italic"><span style="color: #9A1900">//init_flow_graph</span></span>
                <span style="color: #008080">dinic&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">d</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                <span style="color: #009900">int</span> s <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> t <span style="color: #990000">=</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>vis_edge<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>vis_edge<span style="color: #990000">));</span>
                <span style="font-weight: bold"><span style="color: #000000">init_graph_dfs</span></span><span style="color: #990000">(</span>d<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
                ans1 <span style="color: #990000">=</span> d<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">max_flow</span></span><span style="color: #990000">(</span>s<span style="color: #990000">,</span> t<span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> ans1<span style="color: #990000">,</span> ans2<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_tree_heavy_light_decomposition_segment_tree_poj3237_cc_code">26. <code>graph/tree/heavy-light-decomposition/segment-tree/poj3237.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// opt: 0</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// flag: -g</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cassert&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">28</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">20007</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> top<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> tid<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> id<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> size<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> depth<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> parent<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> heavy_son<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>

<span style="color: #009900">int</span> end_point<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> next<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> head<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> weight<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> edge_alloc<span style="color: #990000">;</span>

<span style="color: #009900">int</span> cost<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> dfs_tick<span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        edge_alloc <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>head<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>head<span style="color: #990000">));</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">,</span> <span style="color: #009900">int</span> c<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        end_point<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span> weight<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> c<span style="color: #990000">;</span> next<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> head<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> edge_alloc<span style="color: #990000">++;</span>
        end_point<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> u<span style="color: #990000">;</span> weight<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> c<span style="color: #990000">;</span> next<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> head<span style="color: #990000">[</span>v<span style="color: #990000">];</span> head<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> edge_alloc<span style="color: #990000">++;</span>
<span style="color: #FF0000">}</span>


<span style="color: #009900">int</span> seg_max<span style="color: #990000">[</span><span style="color: #993399">4</span> <span style="color: #990000">*</span> maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> seg_min<span style="color: #990000">[</span><span style="color: #993399">4</span> <span style="color: #990000">*</span> maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> color<span style="color: #990000">[</span><span style="color: #993399">4</span> <span style="color: #990000">*</span> maxn<span style="color: #990000">];</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">push_down</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> id<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> tl <span style="color: #990000">=</span> id <span style="color: #990000">*</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> tr <span style="color: #990000">=</span> tl <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        color<span style="color: #990000">[</span>tl<span style="color: #990000">]</span> <span style="color: #990000">^=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        color<span style="color: #990000">[</span>tr<span style="color: #990000">]</span> <span style="color: #990000">^=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        color<span style="color: #990000">[</span>id<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">push_up</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> id<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> tl <span style="color: #990000">=</span> id <span style="color: #990000">*</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> tr <span style="color: #990000">=</span> tl <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #009900">int</span> tmax <span style="color: #990000">=</span> <span style="color: #990000">-</span>inf<span style="color: #990000">,</span> tmin <span style="color: #990000">=</span> inf<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>color<span style="color: #990000">[</span>tl<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                  tmax <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>tmax<span style="color: #990000">,</span> <span style="color: #990000">-</span>seg_min<span style="color: #990000">[</span>tl<span style="color: #990000">]);</span>
                  tmin <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>tmin<span style="color: #990000">,</span> <span style="color: #990000">-</span>seg_max<span style="color: #990000">[</span>tl<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                tmax <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>tmax<span style="color: #990000">,</span> seg_max<span style="color: #990000">[</span>tl<span style="color: #990000">]);</span>
                tmin <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>tmin<span style="color: #990000">,</span> seg_min<span style="color: #990000">[</span>tl<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>color<span style="color: #990000">[</span>tr<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                  tmax <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>tmax<span style="color: #990000">,</span> <span style="color: #990000">-</span>seg_min<span style="color: #990000">[</span>tr<span style="color: #990000">]);</span>
                  tmin <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>tmin<span style="color: #990000">,</span> <span style="color: #990000">-</span>seg_max<span style="color: #990000">[</span>tr<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                tmax <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>tmax<span style="color: #990000">,</span> seg_max<span style="color: #990000">[</span>tr<span style="color: #990000">]);</span>
                tmin <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>tmin<span style="color: #990000">,</span> seg_min<span style="color: #990000">[</span>tr<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>
        seg_max<span style="color: #990000">[</span>id<span style="color: #990000">]</span> <span style="color: #990000">=</span> tmax<span style="color: #990000">;</span>
        seg_min<span style="color: #990000">[</span>id<span style="color: #990000">]</span> <span style="color: #990000">=</span> tmin<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> id<span style="color: #990000">,</span> <span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">,</span> <span style="color: #009900">int</span> x<span style="color: #990000">,</span> <span style="color: #009900">int</span> y<span style="color: #990000">,</span> <span style="color: #009900">int</span> val<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">&lt;=</span> l <span style="color: #990000">&amp;&amp;</span> r <span style="color: #990000">&lt;=</span> y<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> seg_min<span style="color: #990000">[</span>id<span style="color: #990000">]</span> <span style="color: #990000">=</span> seg_max<span style="color: #990000">[</span>id<span style="color: #990000">]</span> <span style="color: #990000">=</span> val<span style="color: #990000">;</span> color<span style="color: #990000">[</span>id<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>color<span style="color: #990000">[</span>id<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #000000">push_down</span></span><span style="color: #990000">(</span>id<span style="color: #990000">);</span>
        <span style="color: #009900">int</span> tl <span style="color: #990000">=</span> id <span style="color: #990000">*</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> tr <span style="color: #990000">=</span> tl <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> mid <span style="color: #990000">=</span> <span style="color: #990000">(</span>l <span style="color: #990000">+</span> r<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">&lt;=</span> mid<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span>tl<span style="color: #990000">,</span> l<span style="color: #990000">,</span> mid<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> val<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>mid <span style="color: #990000">&lt;</span> y<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> mid <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> r<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> val<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #000000">push_up</span></span><span style="color: #990000">(</span>id<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> id<span style="color: #990000">,</span> <span style="color: #009900">int</span> val<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> u <span style="color: #990000">=</span> end_point<span style="color: #990000">[</span>id<span style="color: #990000">],</span> v <span style="color: #990000">=</span> end_point<span style="color: #990000">[</span>id <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">];</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>parent<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">==</span> v<span style="color: #990000">)</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>v<span style="color: #990000">],</span> tid<span style="color: #990000">[</span>v<span style="color: #990000">],</span> val<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">seg_negate</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> id<span style="color: #990000">,</span> <span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">,</span> <span style="color: #009900">int</span> x<span style="color: #990000">,</span> <span style="color: #009900">int</span> y<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
<span style="font-style: italic"><span style="color: #9A1900">//      std::cout &lt;&lt; id &lt;&lt; ' ' &lt;&lt; l &lt;&lt; ' ' &lt;&lt; r &lt;&lt; ' ' &lt;&lt; x &lt;&lt; ' ' &lt;&lt; y &lt;&lt; '\n';</span></span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">&lt;=</span> l <span style="color: #990000">&amp;&amp;</span> r <span style="color: #990000">&lt;=</span> y<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> color<span style="color: #990000">[</span>id<span style="color: #990000">]</span> <span style="color: #990000">^=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>

        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>color<span style="color: #990000">[</span>id<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #000000">push_down</span></span><span style="color: #990000">(</span>id<span style="color: #990000">);</span>
        <span style="color: #009900">int</span> tl <span style="color: #990000">=</span> id <span style="color: #990000">*</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> tr <span style="color: #990000">=</span> tl <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> mid <span style="color: #990000">=</span> <span style="color: #990000">(</span>l <span style="color: #990000">+</span> r<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">&lt;=</span> mid<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #000000">seg_negate</span></span><span style="color: #990000">(</span>tl<span style="color: #990000">,</span> l<span style="color: #990000">,</span> mid<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>mid <span style="color: #990000">&lt;</span> y<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #000000">seg_negate</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> mid <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> r<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #000000">push_up</span></span><span style="color: #990000">(</span>id<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> id<span style="color: #990000">,</span> <span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">,</span> <span style="color: #009900">int</span> x<span style="color: #990000">,</span> <span style="color: #009900">int</span> y<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">&lt;=</span> l <span style="color: #990000">&amp;&amp;</span> r <span style="color: #990000">&lt;=</span> y<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>color<span style="color: #990000">[</span>id<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">-</span>seg_min<span style="color: #990000">[</span>id<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> seg_max<span style="color: #990000">[</span>id<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>color<span style="color: #990000">[</span>id<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #000000">push_down</span></span><span style="color: #990000">(</span>id<span style="color: #990000">);</span>
        <span style="color: #009900">int</span> tl <span style="color: #990000">=</span> id <span style="color: #990000">*</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> tr <span style="color: #990000">=</span> tl <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> mid <span style="color: #990000">=</span> <span style="color: #990000">(</span>l <span style="color: #990000">+</span> r<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> ret <span style="color: #990000">=</span> <span style="color: #990000">-</span>inf<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">&lt;=</span> mid<span style="color: #990000">)</span> ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span>tl<span style="color: #990000">,</span> l<span style="color: #990000">,</span> mid<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">));</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>mid <span style="color: #990000">&lt;</span> y<span style="color: #990000">)</span> ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> mid <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> r<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">));</span>
        <span style="font-weight: bold"><span style="color: #000000">push_up</span></span><span style="color: #990000">(</span>id<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ret<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">query</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> ret <span style="color: #990000">=</span> <span style="color: #990000">-</span>inf<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">!=</span> top<span style="color: #990000">[</span>v<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>depth<span style="color: #990000">[</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]]</span> <span style="color: #990000">&lt;</span> depth<span style="color: #990000">[</span>top<span style="color: #990000">[</span>v<span style="color: #990000">]])</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
                ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]],</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">]));</span>
                u <span style="color: #990000">=</span> parent<span style="color: #990000">[</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]];</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>depth<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> depth<span style="color: #990000">[</span>v<span style="color: #990000">])</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
        ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> tid<span style="color: #990000">[</span>v<span style="color: #990000">]));</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ret<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">negate</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">!=</span> top<span style="color: #990000">[</span>v<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>depth<span style="color: #990000">[</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]]</span> <span style="color: #990000">&lt;</span> depth<span style="color: #990000">[</span>top<span style="color: #990000">[</span>v<span style="color: #990000">]])</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">seg_negate</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]],</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">]);</span>
                u <span style="color: #990000">=</span> parent<span style="color: #990000">[</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]];</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>depth<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> depth<span style="color: #990000">[</span>v<span style="color: #990000">])</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #000000">seg_negate</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> tid<span style="color: #990000">[</span>v<span style="color: #990000">]);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">dfs_size</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> p<span style="color: #990000">,</span> <span style="color: #009900">int</span> d<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        parent<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> p<span style="color: #990000">;</span>
        depth<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> d<span style="color: #990000">;</span>
        size<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="color: #009900">int</span> max_size <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> i<span style="color: #990000">;</span> i <span style="color: #990000">=</span> next<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> v <span style="color: #990000">=</span> end_point<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>v <span style="color: #990000">==</span> p<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                cost<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> weight<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #000000">dfs_size</span></span><span style="color: #990000">(</span>v<span style="color: #990000">,</span> u<span style="color: #990000">,</span> d <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                size<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+=</span> size<span style="color: #990000">[</span>v<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>size<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> max_size<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        max_size <span style="color: #990000">=</span> size<span style="color: #990000">[</span>v<span style="color: #990000">];</span>
                        heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">dfs_heavy_chain</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> ancestor<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        tid<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">++</span>dfs_tick<span style="color: #990000">;</span>
        top<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> ancestor<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">],</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">],</span> cost<span style="color: #990000">[</span>u<span style="color: #990000">]);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #000000">dfs_heavy_chain</span></span><span style="color: #990000">(</span>heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">],</span> ancestor<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> i<span style="color: #990000">;</span> i <span style="color: #990000">=</span> next<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> v <span style="color: #990000">=</span> end_point<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>v <span style="color: #990000">==</span> parent<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">||</span> v <span style="color: #990000">==</span> heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">dfs_heavy_chain</span></span><span style="color: #990000">(</span>v<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> T<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>T<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>T<span style="color: #990000">--)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">();</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> c<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>c<span style="color: #990000">);</span>
                        id<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> edge_alloc<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> c<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>

                <span style="font-weight: bold"><span style="color: #000000">dfs_size</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>

                <span style="font-style: italic"><span style="color: #9A1900">//std::fill(seg_min, seg_min + 4 * maxn, inf);</span></span>
                <span style="font-style: italic"><span style="color: #9A1900">//std::fill(seg_max, seg_max + 4 * maxn, -inf);</span></span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>seg_min<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>seg_min<span style="color: #990000">));</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>seg_max<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>seg_max<span style="color: #990000">));</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>color<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>color<span style="color: #990000">));</span>
                cost<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">-</span>inf<span style="color: #990000">;</span>
                dfs_tick <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">dfs_heavy_chain</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>

                <span style="color: #009900">char</span> s<span style="color: #990000">[</span><span style="color: #993399">10</span><span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%s"</span><span style="color: #990000">,</span> s<span style="color: #990000">)</span> <span style="color: #990000">&amp;&amp;</span> s<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">!=</span> <span style="color: #FF0000">'D'</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>s<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #FF0000">'C'</span><span style="color: #990000">)</span>
                                <span style="font-weight: bold"><span style="color: #000000">update</span></span><span style="color: #990000">(</span>id<span style="color: #990000">[</span>x<span style="color: #990000">],</span> y<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>s<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #FF0000">'Q'</span><span style="color: #990000">)</span>
                                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">query</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> y<span style="color: #990000">));</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                                <span style="font-weight: bold"><span style="color: #000000">negate</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> y<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_tree_heavy_light_decomposition_segment_tree_spoj375_cc_code">27. <code>graph/tree/heavy-light-decomposition/segment-tree/spoj375.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;string&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> inf <span style="color: #990000">=</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">27</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">20007</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> parent<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> depth<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> size<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> top<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> tid<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span> <span style="font-style: italic"><span style="color: #9A1900">// edge index after hld</span></span>
<span style="color: #009900">int</span> id<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>  <span style="font-style: italic"><span style="color: #9A1900">// edge index before hld</span></span>
<span style="color: #009900">int</span> heavy_son<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>

<span style="color: #009900">int</span> end_point<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> weight<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> next<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> head<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>

<span style="color: #009900">int</span> cost<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> edge_alloc<span style="color: #990000">,</span> n<span style="color: #990000">,</span> dfs_tick<span style="color: #990000">;</span>

<span style="color: #009900">int</span> seg_tree<span style="color: #990000">[</span><span style="color: #993399">4</span> <span style="color: #990000">*</span> maxn<span style="color: #990000">];</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        edge_alloc <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>head<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>head<span style="color: #990000">));</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">,</span> <span style="color: #009900">int</span> c<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        end_point<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span> weight<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> c<span style="color: #990000">;</span> next<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> head<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> edge_alloc<span style="color: #990000">++;</span>
        end_point<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> u<span style="color: #990000">;</span> weight<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> c<span style="color: #990000">;</span> next<span style="color: #990000">[</span>edge_alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> head<span style="color: #990000">[</span>v<span style="color: #990000">];</span> head<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> edge_alloc<span style="color: #990000">++;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">dfs_size</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> p<span style="color: #990000">,</span> <span style="color: #009900">int</span> d<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        parent<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> p<span style="color: #990000">;</span>
        depth<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> d<span style="color: #990000">;</span>
        size<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="color: #009900">int</span> max_size <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> i<span style="color: #990000">;</span> i <span style="color: #990000">=</span> next<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> v <span style="color: #990000">=</span> end_point<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>v <span style="color: #990000">==</span> p<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                cost<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> weight<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #000000">dfs_size</span></span><span style="color: #990000">(</span>v<span style="color: #990000">,</span> u<span style="color: #990000">,</span> d <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
                size<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+=</span> size<span style="color: #990000">[</span>v<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>size<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> max_size<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        max_size <span style="color: #990000">=</span> size<span style="color: #990000">[</span>v<span style="color: #990000">];</span>
                        heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> id<span style="color: #990000">,</span> <span style="color: #009900">int</span> il<span style="color: #990000">,</span> <span style="color: #009900">int</span> ir<span style="color: #990000">,</span> <span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">,</span> <span style="color: #009900">int</span> val<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// il means interval l, same as ir</span></span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">&lt;=</span> il <span style="color: #990000">&amp;&amp;</span> ir <span style="color: #990000">&lt;=</span> r<span style="color: #990000">)</span> seg_tree<span style="color: #990000">[</span>id<span style="color: #990000">]</span> <span style="color: #990000">=</span> val<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> tl <span style="color: #990000">=</span> <span style="color: #993399">2</span> <span style="color: #990000">*</span> id<span style="color: #990000">,</span> tr <span style="color: #990000">=</span> tl <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="color: #009900">int</span> mid <span style="color: #990000">=</span> <span style="color: #990000">(</span>il <span style="color: #990000">+</span> ir<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">&lt;=</span> mid<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span>tl<span style="color: #990000">,</span> il<span style="color: #990000">,</span> mid<span style="color: #990000">,</span> l<span style="color: #990000">,</span> r<span style="color: #990000">,</span> val<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>mid <span style="color: #990000">&lt;</span> r<span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> mid <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> ir<span style="color: #990000">,</span> l<span style="color: #990000">,</span> r<span style="color: #990000">,</span> val<span style="color: #990000">);</span>
                seg_tree<span style="color: #990000">[</span>id<span style="color: #990000">]</span> <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>seg_tree<span style="color: #990000">[</span>tl<span style="color: #990000">],</span> seg_tree<span style="color: #990000">[</span>tr<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> id<span style="color: #990000">,</span> <span style="color: #009900">int</span> il<span style="color: #990000">,</span> <span style="color: #009900">int</span> ir<span style="color: #990000">,</span> <span style="color: #009900">int</span> l<span style="color: #990000">,</span> <span style="color: #009900">int</span> r<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">&lt;=</span> il <span style="color: #990000">&amp;&amp;</span> ir <span style="color: #990000">&lt;=</span> r<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> seg_tree<span style="color: #990000">[</span>id<span style="color: #990000">];</span>
        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> tl <span style="color: #990000">=</span> id <span style="color: #990000">*</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> tr <span style="color: #990000">=</span> tl <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="color: #009900">int</span> mid <span style="color: #990000">=</span> <span style="color: #990000">(</span>il <span style="color: #990000">+</span> ir<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> ret <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">&lt;=</span> mid<span style="color: #990000">)</span> ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span>tl<span style="color: #990000">,</span> il<span style="color: #990000">,</span> mid<span style="color: #990000">,</span> l<span style="color: #990000">,</span> r<span style="color: #990000">));</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>mid <span style="color: #990000">&lt;</span> r<span style="color: #990000">)</span> ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> mid <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> ir<span style="color: #990000">,</span> l<span style="color: #990000">,</span> r<span style="color: #990000">));</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ret<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">seg_update</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> id<span style="color: #990000">,</span> <span style="color: #009900">int</span> val<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> u <span style="color: #990000">=</span> end_point<span style="color: #990000">[</span>id<span style="color: #990000">],</span> v <span style="color: #990000">=</span> end_point<span style="color: #990000">[</span>id <span style="color: #990000">^</span> <span style="color: #993399">1</span><span style="color: #990000">];</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>parent<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">==</span> v<span style="color: #990000">)</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>v<span style="color: #990000">],</span> tid<span style="color: #990000">[</span>v<span style="color: #990000">],</span> val<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">query</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> ret <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">!=</span> top<span style="color: #990000">[</span>v<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>depth<span style="color: #990000">[</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]]</span> <span style="color: #990000">&lt;</span> depth<span style="color: #990000">[</span>top<span style="color: #990000">[</span>v<span style="color: #990000">]])</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
                ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]],</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">]));</span>
                u <span style="color: #990000">=</span> parent<span style="color: #990000">[</span>top<span style="color: #990000">[</span>u<span style="color: #990000">]];</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>depth<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">&gt;</span> depth<span style="color: #990000">[</span>v<span style="color: #990000">])</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>u<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
        ret <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>ret<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">seg_query</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> tid<span style="color: #990000">[</span>v<span style="color: #990000">]));</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ret<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">dfs_heavy_chain</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> ancestor<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        tid<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">++</span>dfs_tick<span style="color: #990000">;</span>
        top<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> ancestor<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">seg_interval_max_update</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> n<span style="color: #990000">,</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">],</span> tid<span style="color: #990000">[</span>u<span style="color: #990000">],</span> cost<span style="color: #990000">[</span>u<span style="color: #990000">]);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #000000">dfs_heavy_chain</span></span><span style="color: #990000">(</span>heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">],</span> ancestor<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> i<span style="color: #990000">;</span> i <span style="color: #990000">=</span> next<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> v <span style="color: #990000">=</span> end_point<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>v <span style="color: #990000">==</span> parent<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">||</span> v <span style="color: #990000">==</span> heavy_son<span style="color: #990000">[</span>u<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">dfs_heavy_chain</span></span><span style="color: #990000">(</span>v<span style="color: #990000">,</span> v<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> T<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>T<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>T<span style="color: #990000">--)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">();</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> z<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>z<span style="color: #990000">);</span>
                        id<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> edge_alloc<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> z<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>

                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>seg_tree<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>seg_tree<span style="color: #990000">));</span>
                <span style="font-weight: bold"><span style="color: #000000">dfs_size</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
                cost<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">-</span>inf<span style="color: #990000">;</span>
                dfs_tick <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">dfs_heavy_chain</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>

                <span style="color: #009900">char</span> s<span style="color: #990000">[</span><span style="color: #993399">10</span><span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%s"</span><span style="color: #990000">,</span> s<span style="color: #990000">)</span> <span style="color: #990000">&amp;&amp;</span> s<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">!=</span> <span style="color: #FF0000">'D'</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>s<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #FF0000">'C'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #000000">seg_update</span></span><span style="color: #990000">(</span>id<span style="color: #990000">[</span>x<span style="color: #990000">],</span> y<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>                     std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">query</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> y<span style="color: #990000">));</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_graph_undirected_graph_minimum_circle_flyoed_cc_code">28. <code>graph/undirected_graph_minimum_circle(flyoed).cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900">ID: icerupt1</span></span>
<span style="font-style: italic"><span style="color: #9A1900">PROG: fence6</span></span>
<span style="font-style: italic"><span style="color: #9A1900">LANG: C++11</span></span>
<span style="font-style: italic"><span style="color: #9A1900">*/</span></span>

<span style="font-style: italic"><span style="color: #9A1900">//chapter 4 section 1</span></span>
<span style="font-style: italic"><span style="color: #9A1900">/* solution</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> *</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> *</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> *</span></span>
<span style="font-style: italic"><span style="color: #9A1900">*/</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;fstream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>

std<span style="color: #990000">::</span><span style="color: #008080">ifstream</span> fin <span style="color: #FF0000">{</span><span style="color: #FF0000">"fence6.in"</span> <span style="color: #FF0000">}</span><span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">ofstream</span> fout<span style="color: #FF0000">{</span><span style="color: #FF0000">"fence6.out"</span><span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">edge</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> len<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> one_end<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> other_end<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> oo <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">&lt;&lt;</span><span style="color: #993399">27</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">102</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> map<span style="color: #990000">[</span>maxn<span style="color: #990000">][</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> dis<span style="color: #990000">[</span>maxn<span style="color: #990000">][</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>

std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;edge&gt;</span> es<span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;int&gt;&gt;</span> points<span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;std::vector&lt;int&gt;&gt;</span> edge_points<span style="color: #990000">;</span>

<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">equal</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt; const &amp; a, std::vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">!=</span> b<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">())</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #990000">(</span><span style="color: #009900">int</span><span style="color: #990000">)</span>a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>a<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> b<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">less</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt; const &amp; a, std::vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> t <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">(),</span> b<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">());</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> t<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>a<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> b<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>a<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> b<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">&lt;</span> b<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        fin <span style="color: #990000">&gt;&gt;</span> n<span style="color: #990000">;</span>
        es<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        edge_points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> id<span style="color: #990000">,</span> n1<span style="color: #990000">,</span> n2<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                fin <span style="color: #990000">&gt;&gt;</span> id<span style="color: #990000">;</span> id<span style="color: #990000">--;</span>
                fin <span style="color: #990000">&gt;&gt;</span> es<span style="color: #990000">[</span>id<span style="color: #990000">].</span>len <span style="color: #990000">&gt;&gt;</span> n1 <span style="color: #990000">&gt;&gt;</span> n2<span style="color: #990000">;</span>
                es<span style="color: #990000">[</span>id<span style="color: #990000">].</span>one_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n1<span style="color: #990000">);</span>
                es<span style="color: #990000">[</span>id<span style="color: #990000">].</span>other_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n2<span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> points_one_end<span style="color: #990000">;</span>
                std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> points_other_end<span style="color: #990000">;</span>
                points_one_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>id<span style="color: #990000">);</span>
                points_other_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>id<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> id_t<span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> n1<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        fin <span style="color: #990000">&gt;&gt;</span> id_t<span style="color: #990000">;</span> id_t<span style="color: #990000">--;</span>
                        es<span style="color: #990000">[</span>id<span style="color: #990000">].</span>one_end<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> id_t<span style="color: #990000">;</span>
                        points_one_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>id_t<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> id_t<span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> n2<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        fin <span style="color: #990000">&gt;&gt;</span> id_t<span style="color: #990000">;</span> id_t<span style="color: #990000">--;</span>
                        es<span style="color: #990000">[</span>id<span style="color: #990000">].</span>other_end<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> id_t<span style="color: #990000">;</span>
                        points_other_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>id_t<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>points_one_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> points_one_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">());</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>points_other_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> points_other_end<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">());</span>
                points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>points_one_end<span style="color: #990000">);</span>
                points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>points_other_end<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">//deduplicate</span></span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">(),</span> less<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #990000">(</span><span style="color: #009900">int</span><span style="color: #990000">)</span>points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">equal</span></span><span style="color: #990000">(</span>points<span style="color: #990000">[</span>i<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">],</span> points<span style="color: #990000">[</span>i<span style="color: #990000">]))</span>
                        points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">erase</span></span><span style="color: #990000">(</span>points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">()</span> <span style="color: #990000">+</span> <span style="color: #990000">(--</span>i<span style="color: #990000">));</span>

        m <span style="color: #990000">=</span> points<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> j <span style="color: #990000">:</span> points<span style="color: #990000">[</span>i<span style="color: #990000">])</span>
                        edge_points<span style="color: #990000">[</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>i<span style="color: #990000">);</span>

        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fill</span></span><span style="color: #990000">(</span>map<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> map<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">+</span> maxn<span style="color: #990000">*</span>maxn<span style="color: #990000">,</span> oo<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> x <span style="color: #990000">=</span> edge_points<span style="color: #990000">[</span>i<span style="color: #990000">][</span><span style="color: #993399">0</span><span style="color: #990000">],</span> y <span style="color: #990000">=</span> edge_points<span style="color: #990000">[</span>i<span style="color: #990000">][</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
                map<span style="color: #990000">[</span>x<span style="color: #990000">][</span>y<span style="color: #990000">]</span> <span style="color: #990000">=</span> map<span style="color: #990000">[</span>y<span style="color: #990000">][</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> es<span style="color: #990000">[</span>i<span style="color: #990000">].</span>len<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> dis<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> map<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">];</span>


        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>map<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">&gt;=</span> oo<span style="color: #990000">)</span> std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"* "</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>                             std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> map<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>


        <span style="color: #009900">int</span> ans <span style="color: #990000">=</span> oo<span style="color: #990000">;</span>

        <span style="font-style: italic"><span style="color: #9A1900">//core part flyoed</span></span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> k <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> k <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> k<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> k<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> j <span style="color: #990000">=</span> i<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> k<span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
                                ans <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>ans<span style="color: #990000">,</span> map<span style="color: #990000">[</span>i<span style="color: #990000">][</span>k<span style="color: #990000">]</span> <span style="color: #990000">+</span> map<span style="color: #990000">[</span>k<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">+</span> dis<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">]);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
                                dis<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>dis<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">],</span> dis<span style="color: #990000">[</span>i<span style="color: #990000">][</span>k<span style="color: #990000">]</span> <span style="color: #990000">+</span> dis<span style="color: #990000">[</span>k<span style="color: #990000">][</span>j<span style="color: #990000">]);</span>
        <span style="color: #FF0000">}</span>


        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>dis<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">&gt;=</span> oo<span style="color: #990000">)</span> std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"* "</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>                             std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> dis<span style="color: #990000">[</span>i<span style="color: #990000">][</span>j<span style="color: #990000">]</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>


        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> ans <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        fout <span style="color: #990000">&lt;&lt;</span> ans <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_input_input_output_optimization_cc_code">29. <code>input/input_output_optimization.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; test.in</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>

<span style="font-style: italic"><span style="color: #9A1900">//#define HUGE_INPUT</span></span>

<span style="font-weight: bold"><span style="color: #000080">#ifndef</span></span> HUGE_INPUT
<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">scan</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> <span style="color: #990000">&amp;</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        a <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
        <span style="color: #009900">bool</span> flag <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="color: #009900">char</span> ch <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">getchar</span></span><span style="color: #990000">();</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>ch <span style="color: #990000">==</span> <span style="color: #FF0000">'-'</span><span style="color: #990000">)</span>
                flag <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>ch <span style="color: #990000">&gt;=</span> <span style="color: #FF0000">'0'</span> <span style="color: #990000">&amp;&amp;</span> ch <span style="color: #990000">&lt;=</span> <span style="color: #FF0000">'9'</span><span style="color: #990000">)</span>
                a <span style="color: #990000">=</span> ch <span style="color: #990000">-</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">((</span>ch <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">getchar</span></span><span style="color: #990000">())</span> <span style="color: #990000">&gt;=</span> <span style="color: #FF0000">'0'</span> <span style="color: #990000">&amp;&amp;</span> ch <span style="color: #990000">&lt;=</span> <span style="color: #FF0000">'9'</span><span style="color: #990000">)</span>
                a <span style="color: #990000">=</span> a <span style="color: #990000">*</span> <span style="color: #993399">10</span> <span style="color: #990000">+</span> ch <span style="color: #990000">-</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>flag<span style="color: #990000">)</span> a <span style="color: #990000">=</span> <span style="color: #990000">-</span>a<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #000080">#endif</span></span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">print</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>a <span style="color: #990000">&gt;</span> <span style="color: #993399">9</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #000000">print</span></span><span style="color: #990000">(</span>a <span style="color: #990000">/</span> <span style="color: #993399">10</span><span style="color: #990000">);</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">putchar</span></span><span style="color: #990000">(</span>a <span style="color: #990000">%</span> <span style="color: #993399">10</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #000080">#ifdef</span></span> HUGE_INPUT
<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #009900">int</span> BZ <span style="color: #990000">=</span> <span style="color: #993399">30</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">20</span><span style="color: #990000">;</span>
<span style="color: #009900">char</span> Buf<span style="color: #990000">[</span>BZ <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">],</span> <span style="color: #990000">*</span>buf <span style="color: #990000">=</span> Buf<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">inline</span></span> <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">scan</span></span><span style="color: #990000">(</span><span style="color: #008080">T</span> <span style="color: #990000">&amp;</span>a<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// method2: for huge input</span></span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">bool</span> flag <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>a <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&lt;</span> <span style="color: #FF0000">'0'</span> <span style="color: #990000">||</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&gt;</span> <span style="color: #FF0000">'9'</span><span style="color: #990000">;</span> <span style="color: #990000">++</span>buf<span style="color: #990000">)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(*</span>buf <span style="color: #990000">==</span> <span style="color: #FF0000">'-'</span><span style="color: #990000">)</span> flag <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&gt;=</span> <span style="color: #FF0000">'0'</span> <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&lt;=</span> <span style="color: #FF0000">'9'</span><span style="color: #990000">;</span> buf<span style="color: #990000">++)</span>
                a <span style="color: #990000">=</span> a <span style="color: #990000">*</span> <span style="color: #993399">10</span> <span style="color: #990000">+</span> <span style="color: #990000">(*</span>buf <span style="color: #990000">-</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>flag<span style="color: #990000">)</span> a <span style="color: #990000">=</span> <span style="color: #990000">-</span>a<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #000080">#endif</span></span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #000080">#ifdef</span></span> HUGE_INPUT
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fread</span></span><span style="color: #990000">(</span>Buf<span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> BZ<span style="color: #990000">,</span> stdin<span style="color: #990000">);</span> <span style="font-style: italic"><span style="color: #9A1900">// method2</span></span>
<span style="font-weight: bold"><span style="color: #000080">#endif</span></span>

        <span style="color: #009900">int</span> n<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">scan</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #000000">print</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_math_baby_step_giant_step_v0_cc_code">30. <code>math/baby-step-giant-step_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// poj2417</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：求A^x == B(mod P)，A, B, P给定，P是素数，求最小x。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：baby step giant step, bsgs.</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       将x拆分，x = i * m + j, 那么</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       A^x == B(mod P) ==&gt; A^(i*m) * A^j == B(mod P） 也即</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       D^i * A^j == B(mod p)，其中 D = A^m，预先处理出每个A^j (j [0, m))</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       塞到hash表里。现在只需要对从小到大枚举i，那么相当于求解</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       A' * X == b(mod P)，其中A' = D^i, X = A^j，这是单元线性模方程，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       这道题P是素数，比较特殊（好做）。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       解出X之后判断hash表里有对应的A^j，如果有答案就是对应的i * m + j，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       否则继续。一直没有就没有解。分析下时间复杂度，共进行n/m次小步，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       O( (n/m)*(log(P)+hash) + m)，取m=sqrt(n)左右比较合适，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       复杂度为O( sqrt(n)*k ), 可以理解下n=p。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cmath&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">long</span> <span style="color: #009900">long</span> value_type<span style="color: #990000">;</span>
<span style="color: #008080">value_type</span> a<span style="color: #990000">,</span> b<span style="color: #990000">,</span> p<span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// a ^ x == b (mod p)</span></span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> hash
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxlen <span style="color: #990000">=</span> <span style="color: #993399">1299721</span><span style="color: #990000">;</span>
        <span style="color: #009900">int</span> hashed<span style="color: #990000">[</span>maxlen<span style="color: #990000">];</span>
        <span style="color: #008080">value_type</span> key<span style="color: #990000">[</span>maxlen<span style="color: #990000">];</span>
        <span style="color: #009900">int</span> value<span style="color: #990000">[</span>maxlen<span style="color: #990000">];</span>
        <span style="color: #009900">int</span> timestamp <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> timestamp<span style="color: #990000">++;</span> <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> k<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> i <span style="color: #990000">=</span> k <span style="color: #990000">%</span> maxlen<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> timestamp <span style="color: #990000">&amp;&amp;</span> key<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> k<span style="color: #990000">;</span> i <span style="color: #990000">=</span> <span style="color: #990000">(</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> maxlen<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> timestamp<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span> key<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> k<span style="color: #990000">;</span> value<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> k<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">value_type</span> i <span style="color: #990000">=</span> k <span style="color: #990000">%</span> maxlen<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> timestamp <span style="color: #990000">&amp;&amp;</span> key<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> k<span style="color: #990000">;</span> i <span style="color: #990000">=</span> <span style="color: #990000">(</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> maxlen<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> timestamp<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> value<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">euler_totient</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// this case x is a prime</span></span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> x <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> a<span style="color: #990000">,</span>  <span style="color: #008080">value_type</span> b<span style="color: #990000">,</span> value_type <span style="color: #990000">&amp;</span> d<span style="color: #990000">,</span> value_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> value_type <span style="color: #990000">&amp;</span> y<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>b<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> d <span style="color: #990000">=</span> a<span style="color: #990000">;</span> x <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> y <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span>b<span style="color: #990000">,</span> a <span style="color: #990000">%</span> b<span style="color: #990000">,</span> d<span style="color: #990000">,</span> y<span style="color: #990000">,</span> x<span style="color: #990000">);</span>
                y <span style="color: #990000">-=</span> x <span style="color: #990000">*</span> <span style="color: #990000">(</span>a<span style="color: #990000">/</span>b<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="font-style: italic"><span style="color: #9A1900">// univariate_modular_system_of_linear_equation</span></span>
<span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">umsole</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> b<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> p<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-style: italic"><span style="color: #9A1900">// a*x  is congruent to  b  modulo  p</span></span>
        <span style="font-style: italic"><span style="color: #9A1900">// =&gt; a*x + p*(-y) = b</span></span>
        <span style="color: #008080">value_type</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> b0<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> p<span style="color: #990000">,</span> b0<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">);</span>
        <span style="color: #008080">value_type</span> m <span style="color: #990000">=</span> p <span style="color: #990000">/</span> b0<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">((</span>x <span style="color: #990000">*</span> <span style="color: #990000">(</span>b <span style="color: #990000">/</span> b0<span style="color: #990000">))</span> <span style="color: #990000">%</span> m <span style="color: #990000">+</span> m<span style="color: #990000">)</span> <span style="color: #990000">%</span> m<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">baby_step_giant_step</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> b<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> p<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #008080">value_type</span> m <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">ceil</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sqrt</span></span><span style="color: #990000">((</span><span style="color: #009900">double</span><span style="color: #990000">)</span><span style="font-weight: bold"><span style="color: #000000">euler_totient</span></span><span style="color: #990000">(</span>p<span style="color: #990000">)));</span>
        hash<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">();</span>
        <span style="color: #008080">value_type</span> d <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                hash<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span>d<span style="color: #990000">,</span> i<span style="color: #990000">);</span>
                d <span style="color: #990000">=</span> <span style="color: #990000">(</span>d <span style="color: #990000">*</span> a<span style="color: #990000">)</span> <span style="color: #990000">%</span> p<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// now d = a^m</span></span>
        <span style="color: #008080">value_type</span> di <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// d^i</span></span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #008080">value_type</span> x <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">umsole</span></span><span style="color: #990000">(</span>di<span style="color: #990000">,</span> b<span style="color: #990000">,</span> p<span style="color: #990000">);</span>
                <span style="color: #009900">int</span> tmp  <span style="color: #990000">=</span> hash<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tmp <span style="color: #990000">!=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> i <span style="color: #990000">*</span> m <span style="color: #990000">+</span> tmp<span style="color: #990000">;</span>
                di <span style="color: #990000">=</span> <span style="color: #990000">(</span>di <span style="color: #990000">*</span> d<span style="color: #990000">)</span> <span style="color: #990000">%</span> p<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%lld %lld %lld"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>p<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>a<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>b<span style="color: #990000">)</span> <span style="color: #990000">!=</span> EOF<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                <span style="color: #008080">value_type</span> ans <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">baby_step_giant_step</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> b<span style="color: #990000">,</span> p<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>ans <span style="color: #990000">==</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"no solution</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>               std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%lld</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> ans<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_math_extended_baby_step_giant_step_v0_cc_code">31. <code>math/extended-baby-step-giant-step_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// bzoj1467</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：求A^x==b(mod p) 最小的x，p不一定是素数。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：如果A与p不互质，不断除以gcd(A, p)，其中如果b不是gcd(A, p)的</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       倍数，则不解。不然假设除了k个gcd(A, p)，每个记为di, 记D=d1*d2*..*dk。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       那么A^k/D * A^(x-k) == b/D(mod p/D)，然后吧A^k/D做逆元乘到B那面，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       在做个baby step giant step就行啦。对于x&lt;=k的，先预处理log(p)个。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 统计：276ms，50min，2wa</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cmath&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">long</span> <span style="color: #009900">long</span> v_type<span style="color: #990000">;</span>
<span style="color: #008080">v_type</span> a<span style="color: #990000">,</span> b<span style="color: #990000">,</span> p<span style="color: #990000">;</span>

<span style="color: #008080">v_type</span> <span style="font-weight: bold"><span style="color: #000000">gcd</span></span><span style="color: #990000">(</span><span style="color: #008080">v_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(!</span>b<span style="color: #990000">)</span> <span style="color: #990000">?</span> a <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">gcd</span></span><span style="color: #990000">(</span>b<span style="color: #990000">,</span> a <span style="color: #990000">%</span> b<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> hash
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxlen <span style="color: #990000">=</span> <span style="color: #993399">1299721</span><span style="color: #990000">;</span>
        <span style="color: #009900">int</span> hashed<span style="color: #990000">[</span>maxlen<span style="color: #990000">];</span>
        <span style="color: #008080">v_type</span> key<span style="color: #990000">[</span>maxlen<span style="color: #990000">];</span>
        <span style="color: #009900">int</span> value<span style="color: #990000">[</span>maxlen<span style="color: #990000">];</span>
        <span style="color: #009900">int</span> timestamp <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> timestamp<span style="color: #990000">++;</span> <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span><span style="color: #008080">v_type</span> k<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> i <span style="color: #990000">=</span> k <span style="color: #990000">%</span> maxlen<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> timestamp <span style="color: #990000">&amp;&amp;</span> key<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> k<span style="color: #990000">;</span> i <span style="color: #990000">=</span> <span style="color: #990000">(</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> maxlen<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> timestamp<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span> key<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> k<span style="color: #990000">;</span> value<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span><span style="color: #008080">v_type</span> k<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #008080">v_type</span> i <span style="color: #990000">=</span> k <span style="color: #990000">%</span> maxlen<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> timestamp <span style="color: #990000">&amp;&amp;</span> key<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> k<span style="color: #990000">;</span> i <span style="color: #990000">=</span> <span style="color: #990000">(</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #990000">%</span> maxlen<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>hashed<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> timestamp<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> value<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #008080">v_type</span> <span style="font-weight: bold"><span style="color: #000000">euler_totient</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> x<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// is x prime? FIXME</span></span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> x <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span><span style="color: #008080">v_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> b<span style="color: #990000">,</span> v_type <span style="color: #990000">&amp;</span> d<span style="color: #990000">,</span> v_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> v_type <span style="color: #990000">&amp;</span> y<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>b<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> d <span style="color: #990000">=</span> a<span style="color: #990000">;</span> x <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> y <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span>b<span style="color: #990000">,</span> a <span style="color: #990000">%</span> b<span style="color: #990000">,</span> d<span style="color: #990000">,</span> y<span style="color: #990000">,</span> x<span style="color: #990000">);</span>
                y <span style="color: #990000">-=</span> x <span style="color: #990000">*</span> <span style="color: #990000">(</span>a<span style="color: #990000">/</span>b<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #008080">v_type</span> <span style="font-weight: bold"><span style="color: #000000">inverse</span></span><span style="color: #990000">(</span><span style="color: #008080">v_type</span> t<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> p<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #008080">v_type</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> d<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span>t<span style="color: #990000">,</span> p<span style="color: #990000">,</span> d<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>x <span style="color: #990000">%</span> p <span style="color: #990000">+</span> p<span style="color: #990000">)</span> <span style="color: #990000">%</span> p<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="font-style: italic"><span style="color: #9A1900">// univariate_modular_system_of_linear_equation</span></span>
<span style="color: #008080">v_type</span> <span style="font-weight: bold"><span style="color: #000000">umsole</span></span><span style="color: #990000">(</span><span style="color: #008080">v_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> b<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> p<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-style: italic"><span style="color: #9A1900">// a*x  is congruent to  b  modulo  p</span></span>
        <span style="font-style: italic"><span style="color: #9A1900">// =&gt; a*x + p*(-y) = b</span></span>
        <span style="color: #008080">v_type</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> b0<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> p<span style="color: #990000">,</span> b0<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">);</span>
        <span style="color: #008080">v_type</span> m <span style="color: #990000">=</span> p <span style="color: #990000">/</span> b0<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">((</span>x <span style="color: #990000">*</span> <span style="color: #990000">(</span>b <span style="color: #990000">/</span> b0<span style="color: #990000">))</span> <span style="color: #990000">%</span> m <span style="color: #990000">+</span> m<span style="color: #990000">)</span> <span style="color: #990000">%</span> m<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #008080">v_type</span> <span style="font-weight: bold"><span style="color: #000000">baby_step_giant_step</span></span><span style="color: #990000">(</span><span style="color: #008080">v_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> b<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> p<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #008080">v_type</span> m <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">ceil</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sqrt</span></span><span style="color: #990000">((</span><span style="color: #009900">double</span><span style="color: #990000">)</span><span style="font-weight: bold"><span style="color: #000000">euler_totient</span></span><span style="color: #990000">(</span>p<span style="color: #990000">)));</span>
        hash<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">();</span>
        <span style="color: #008080">v_type</span> d <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                hash<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span>d<span style="color: #990000">,</span> i<span style="color: #990000">);</span>
                d <span style="color: #990000">=</span> <span style="color: #990000">(</span>d <span style="color: #990000">*</span> a<span style="color: #990000">)</span> <span style="color: #990000">%</span> p<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>

        <span style="font-style: italic"><span style="color: #9A1900">// now d = a^m</span></span>
        <span style="color: #008080">v_type</span> di <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// d^i</span></span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="color: #008080">v_type</span> x <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">umsole</span></span><span style="color: #990000">(</span>di<span style="color: #990000">,</span> b<span style="color: #990000">,</span> p<span style="color: #990000">);</span>
                <span style="color: #009900">int</span> tmp <span style="color: #990000">=</span> hash<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tmp <span style="color: #990000">!=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> i <span style="color: #990000">*</span> m <span style="color: #990000">+</span> tmp<span style="color: #990000">;</span>
                di <span style="color: #990000">=</span> <span style="color: #990000">(</span>di <span style="color: #990000">*</span> d<span style="color: #990000">)</span> <span style="color: #990000">%</span> p<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #008080">v_type</span> <span style="font-weight: bold"><span style="color: #000000">quick</span></span><span style="color: #990000">(</span><span style="color: #008080">v_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> b<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> p<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>b<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="color: #008080">v_type</span> ret <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">quick</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> b<span style="color: #990000">/</span><span style="color: #993399">2</span><span style="color: #990000">,</span> p<span style="color: #990000">);</span>
        ret <span style="color: #990000">=</span> <span style="color: #990000">(</span>ret <span style="color: #990000">*</span> ret<span style="color: #990000">)</span> <span style="color: #990000">%</span> p<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>b <span style="color: #990000">&amp;</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> ret <span style="color: #990000">*=</span> a<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ret <span style="color: #990000">%</span> p<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #008080">v_type</span> <span style="font-weight: bold"><span style="color: #000000">extended_baby_step_giant_step</span></span><span style="color: #990000">(</span><span style="color: #008080">v_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> b<span style="color: #990000">,</span> <span style="color: #008080">v_type</span> p<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #008080">v_type</span> tmp <span style="color: #990000">=</span> <span style="color: #993399">1</span> <span style="color: #990000">%</span> p<span style="color: #990000">,</span> k <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> td <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> ta<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #993399">100</span><span style="color: #990000">;</span> i<span style="color: #990000">++,</span> tmp <span style="color: #990000">=</span> <span style="color: #990000">(</span>tmp <span style="color: #990000">*</span> a<span style="color: #990000">)</span> <span style="color: #990000">%</span> p<span style="color: #990000">)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tmp <span style="color: #990000">==</span> b<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> i<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #008080">v_type</span> d<span style="color: #990000">;</span> <span style="color: #990000">(</span>d <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">gcd</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> p<span style="color: #990000">))</span> <span style="color: #990000">!=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> b <span style="color: #990000">/=</span> d<span style="color: #990000">,</span> p <span style="color: #990000">/=</span> d<span style="color: #990000">,</span> td <span style="color: #990000">*=</span> d<span style="color: #990000">,</span> k<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>b <span style="color: #990000">%</span> d<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>

        ta <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">quick</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> k<span style="color: #990000">,</span> p<span style="color: #990000">);</span>

        <span style="color: #008080">v_type</span> tt <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">inverse</span></span><span style="color: #990000">((</span>ta <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">inverse</span></span><span style="color: #990000">(</span>td<span style="color: #990000">,</span> p<span style="color: #990000">))</span> <span style="color: #990000">%</span> p<span style="color: #990000">,</span> p<span style="color: #990000">);</span>
        tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">baby_step_giant_step</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> <span style="color: #990000">(</span>b <span style="color: #990000">*</span> tt<span style="color: #990000">)</span> <span style="color: #990000">%</span> p<span style="color: #990000">,</span> p<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tmp <span style="color: #990000">==</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> tmp <span style="color: #990000">+</span> k<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> a <span style="color: #990000">&gt;&gt;</span> p <span style="color: #990000">&gt;&gt;</span> b <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">(</span>a <span style="color: #990000">||</span> b <span style="color: #990000">||</span> p<span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                b <span style="color: #990000">%=</span> p<span style="color: #990000">;</span> a <span style="color: #990000">%=</span> p<span style="color: #990000">;</span>
                <span style="color: #008080">v_type</span> ans <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">extended_baby_step_giant_step</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> b<span style="color: #990000">,</span> p<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>ans <span style="color: #990000">==</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span>
                        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"No Solution</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> ans <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_math_game_poj1740_cc_code">32. <code>math/game/poj1740.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// 题意：给定n个石头堆，每次可以选取一堆移除至少一个石子，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       再可以选择将这堆剩余石头任意移动若干个到任意其余还有石头的堆中</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       （可以不移动），问在最好的策略下，是不是先手必胜。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：必败态是这样的局势，n为偶数且石头个数都成对出现。其余状态是必胜态。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       首先对于一个必败态，一次移动肯定进入了必胜态，可以这样考虑：</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       i）如果移掉了一堆，那么n变成奇数，进入必胜态;</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       ii）否则，对于这堆对应相同的堆，这次移动当前堆数目肯定减小，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//           其余堆只会增加，所以也进入必胜态。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       那么对于一个必胜态，我们一次移动可以进入必败态，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       i）我们不管已经两两成对的堆，把不成对的堆拿出来单独考虑，肯定是取</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//          个数最多的堆，如果现在堆数是奇数就得取完它。将这些排个序，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//          想成一个柱状图，那么每相邻两个柱子，低的要补成高的，差值投影到</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//          最高的那个柱子发现肯定是够的，所以就这样补齐就好，至于多出来的，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//          直接移除掉就行。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       考虑边界比如一堆，两堆相同的，两堆不同的就很容易看出哪个是必胜，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       哪个是必败。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">16</span><span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> da<span style="color: #990000">;</span>
<span style="color: #009900">int</span> n<span style="color: #990000">;</span>

<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">lose_state</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span><span style="color: #009900">int</span><span style="color: #990000">&gt;</span> <span style="color: #990000">&amp;</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">());</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">&amp;</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> <span style="color: #990000">(</span><span style="color: #009900">int</span><span style="color: #990000">)</span>a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> i <span style="color: #990000">+=</span> <span style="color: #993399">2</span><span style="color: #990000">)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>a<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> a<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span>ios<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sync_with_stdio</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
        da<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">reserve</span></span><span style="color: #990000">(</span>maxn<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n <span style="color: #990000">&amp;&amp;</span> n<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                da<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> da<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #990000">!</span><span style="font-weight: bold"><span style="color: #000000">lose_state</span></span><span style="color: #990000">(</span>da<span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_math_gauss_hdu5544_cc_code">33. <code>math/gauss/hdu5544.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// hdu5544</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：给定一个无向图，然后求最大的异或的和圈。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：先dfs弄出所有环的xor和，然后求出他们的线性基，然后</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       从最高为到最低位把基算进去就是最大值。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 统计：2090ms</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstring&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">50100</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxm <span style="color: #990000">=</span> <span style="color: #993399">100100</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> next<span style="color: #990000">[</span><span style="color: #993399">2</span> <span style="color: #990000">*</span> maxm<span style="color: #990000">],</span> head<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> end_point<span style="color: #990000">[</span><span style="color: #993399">2</span> <span style="color: #990000">*</span> maxm<span style="color: #990000">];</span>
<span style="color: #009900">long</span> <span style="color: #009900">long</span> cost<span style="color: #990000">[</span><span style="color: #993399">2</span> <span style="color: #990000">*</span> maxm<span style="color: #990000">];</span>
<span style="color: #009900">int</span> alloc<span style="color: #990000">;</span>

<span style="color: #009900">int</span> vis<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> time_stamp <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
<span style="color: #009900">long</span> <span style="color: #009900">long</span> xs_to_root<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>

<span style="color: #009900">long</span> <span style="color: #009900">long</span> circle<span style="color: #990000">[</span><span style="color: #993399">2</span> <span style="color: #990000">*</span> maxm<span style="color: #990000">];</span>
<span style="color: #009900">int</span> len<span style="color: #990000">;</span>

<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">,</span> <span style="color: #009900">long</span> <span style="color: #009900">long</span> c<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        end_point<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span> cost<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> c<span style="color: #990000">;</span> next<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> head<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> alloc<span style="color: #990000">++;</span>
        end_point<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> u<span style="color: #990000">;</span> cost<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> c<span style="color: #990000">;</span> next<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> head<span style="color: #990000">[</span>v<span style="color: #990000">];</span> head<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> alloc<span style="color: #990000">++;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">dfs</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">long</span> <span style="color: #009900">long</span> sum<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        xs_to_root<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> sum<span style="color: #990000">;</span>
        vis<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> time_stamp<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> p <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> p<span style="color: #990000">;</span> p <span style="color: #990000">=</span> next<span style="color: #990000">[</span>p<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> v <span style="color: #990000">=</span> end_point<span style="color: #990000">[</span>p<span style="color: #990000">];</span>
                <span style="color: #009900">long</span> <span style="color: #009900">long</span> c <span style="color: #990000">=</span> cost<span style="color: #990000">[</span>p<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>vis<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">==</span> time_stamp<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">long</span> <span style="color: #009900">long</span> tmp <span style="color: #990000">=</span> xs_to_root<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">^</span> sum <span style="color: #990000">^</span> c<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tmp<span style="color: #990000">)</span> circle<span style="color: #990000">[++</span>len<span style="color: #990000">]</span> <span style="color: #990000">=</span> tmp<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
                        <span style="font-weight: bold"><span style="color: #000000">dfs</span></span><span style="color: #990000">(</span>v<span style="color: #990000">,</span> sum <span style="color: #990000">^</span> c<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> max_len <span style="color: #990000">=</span> <span style="color: #993399">61</span><span style="color: #990000">;</span>
<span style="color: #009900">long</span> <span style="color: #009900">long</span> lb<span style="color: #990000">[</span>max_len <span style="color: #990000">+</span> <span style="color: #993399">5</span><span style="color: #990000">];</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">liner_base</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #000000">memset</span></span><span style="color: #990000">(</span>lb<span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>lb<span style="color: #990000">));</span>

        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> len<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> max_len<span style="color: #990000">;</span> j <span style="color: #990000">&gt;=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j<span style="color: #990000">--)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">((</span>circle<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&gt;&gt;</span> j<span style="color: #990000">)</span> <span style="color: #990000">&amp;</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>lb<span style="color: #990000">[</span>j<span style="color: #990000">])</span> <span style="color: #FF0000">{</span> lb<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> circle<span style="color: #990000">[</span>i<span style="color: #990000">];</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> circle<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">^=</span> lb<span style="color: #990000">[</span>j<span style="color: #990000">];</span>
                        <span style="color: #FF0000">}</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        alloc <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        time_stamp<span style="color: #990000">++;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> head<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #009900">int</span> BZ <span style="color: #990000">=</span> <span style="color: #993399">2</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #993399">20</span><span style="color: #990000">;</span>
<span style="color: #009900">char</span> Buf<span style="color: #990000">[</span>BZ <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">],</span> <span style="color: #990000">*</span>buf <span style="color: #990000">=</span> Buf<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">T</span><span style="color: #990000">&gt;</span>
<span style="font-weight: bold"><span style="color: #0000FF">inline</span></span> <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">scan</span></span><span style="color: #990000">(</span><span style="color: #008080">T</span> <span style="color: #990000">&amp;</span>a<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// method2: for huge input</span></span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">bool</span> flag <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>a <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&lt;</span> <span style="color: #FF0000">'0'</span> <span style="color: #990000">||</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&gt;</span> <span style="color: #FF0000">'9'</span><span style="color: #990000">;</span> <span style="color: #990000">++</span>buf<span style="color: #990000">)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(*</span>buf <span style="color: #990000">==</span> <span style="color: #FF0000">'-'</span><span style="color: #990000">)</span> flag <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&gt;=</span> <span style="color: #FF0000">'0'</span> <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">*</span>buf <span style="color: #990000">&lt;=</span> <span style="color: #FF0000">'9'</span><span style="color: #990000">;</span> buf<span style="color: #990000">++)</span>
                a <span style="color: #990000">=</span> a <span style="color: #990000">*</span> <span style="color: #993399">10</span> <span style="color: #990000">+</span> <span style="color: #990000">(*</span>buf <span style="color: #990000">-</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>flag<span style="color: #990000">)</span> a <span style="color: #990000">=</span> <span style="color: #990000">-</span>a<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-style: italic"><span style="color: #9A1900">//   fread(Buf, 1, BZ, stdin); // way2</span></span>

        <span style="color: #009900">int</span> T<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>T<span style="color: #990000">);</span>
        <span style="font-style: italic"><span style="color: #9A1900">// scan(T);</span></span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> ti <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> ti <span style="color: #990000">&lt;=</span> T<span style="color: #990000">;</span> ti<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"Case #%d: "</span><span style="color: #990000">,</span> ti<span style="color: #990000">);</span>

                <span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>m<span style="color: #990000">);</span>
                <span style="font-style: italic"><span style="color: #9A1900">//    scan(n); scan(m);</span></span>
                <span style="font-weight: bold"><span style="color: #000000">init</span></span><span style="color: #990000">();</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">long</span> <span style="color: #009900">long</span> z<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d %lld"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>z<span style="color: #990000">);</span>
                        <span style="font-style: italic"><span style="color: #9A1900">//        scan(x); scan(y); scan(z);</span></span>
                        <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> z<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>

                len <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #000000">dfs</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>

                <span style="font-weight: bold"><span style="color: #000000">liner_base</span></span><span style="color: #990000">();</span>

                <span style="color: #009900">long</span> <span style="color: #009900">long</span> ans <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> max_len<span style="color: #990000">;</span> i <span style="color: #990000">&gt;=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i<span style="color: #990000">--)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!((</span>ans <span style="color: #990000">&gt;&gt;</span> i<span style="color: #990000">)</span> <span style="color: #990000">&amp;</span> <span style="color: #993399">1</span><span style="color: #990000">))</span> ans <span style="color: #990000">^=</span> lb<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%lld</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> ans<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_math_mobius_mobius_g_v0_cc_code">34. <code>math/mobius/mobius_g_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// bzoj2820</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：给定n, m(&lt;=10000000)，求x&lt;=n, y&lt;=m, gcd(x, y)=prime的对数。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：首先还是令</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//        f(i) = (gcd(x, y) = i, x&lt;=n, y&lt;=m)的对数，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//        F(i) = (i | gcd(x, y))的对数。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       那么答案就是sigma(f(p))。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       直接求肯定TLE.</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//                详细见blog吧，在这儿打公式太蛋疼。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 统计：3368ms, 1h, 1a</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">10000005</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> mo<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> g<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> sumg<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span> <span style="font-style: italic"><span style="color: #9A1900">// g(T) = sigma(μ(T/p)) where p is a prime and p | T</span></span>
<span style="color: #009900">bool</span> not_prime<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> prime<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> tot<span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">init_mobius</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> n <span style="color: #990000">=</span> maxn<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        mo<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>not_prime<span style="color: #990000">[</span>i<span style="color: #990000">])</span> prime<span style="color: #990000">[++</span>tot<span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">,</span> mo<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">,</span> g<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> prime<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">*</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> j<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        not_prime<span style="color: #990000">[</span>prime<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">*</span> i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>i <span style="color: #990000">%</span> prime<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                mo<span style="color: #990000">[</span>prime<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">*</span> i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                                g<span style="color: #990000">[</span>prime<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">*</span> i<span style="color: #990000">]</span> <span style="color: #990000">=</span> mo<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                        <span style="color: #FF0000">}</span>
                        mo<span style="color: #990000">[</span>prime<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">*</span> i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">-</span>mo<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                        g<span style="color: #990000">[</span>prime<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">*</span> i<span style="color: #990000">]</span> <span style="color: #990000">=</span> mo<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">-</span> g<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> sumg<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> sumg<span style="color: #990000">[</span>i <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">+</span> g<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">long</span> <span style="color: #009900">long</span> <span style="font-weight: bold"><span style="color: #000000">inverse</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> n<span style="color: #990000">,</span> <span style="color: #009900">int</span> m<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>n <span style="color: #990000">&gt;</span> m<span style="color: #990000">)</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">swap</span></span><span style="color: #990000">(</span>n<span style="color: #990000">,</span> m<span style="color: #990000">);</span>
        <span style="color: #009900">long</span> <span style="color: #009900">long</span> ans <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #009900">int</span> pos <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> pos<span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i <span style="color: #990000">=</span> pos <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                pos <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>n <span style="color: #990000">/</span> <span style="color: #990000">(</span>n<span style="color: #990000">/</span>i<span style="color: #990000">),</span> m <span style="color: #990000">/</span> <span style="color: #990000">(</span>m<span style="color: #990000">/</span>i<span style="color: #990000">));</span>
                ans <span style="color: #990000">+=</span> <span style="color: #990000">(</span><span style="color: #009900">long</span> <span style="color: #009900">long</span><span style="color: #990000">)(</span>sumg<span style="color: #990000">[</span>pos<span style="color: #990000">]</span> <span style="color: #990000">-</span> sumg<span style="color: #990000">[</span>i <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">])</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>n<span style="color: #990000">/</span>i<span style="color: #990000">)</span> <span style="color: #990000">*</span> <span style="color: #990000">(</span>m<span style="color: #990000">/</span>i<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ans<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> T<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>T<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #000000">init_mobius</span></span><span style="color: #990000">();</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>T<span style="color: #990000">--)</span> <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>m<span style="color: #990000">);</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%lld</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">inverse</span></span><span style="color: #990000">(</span>n<span style="color: #990000">,</span> m<span style="color: #990000">));</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_math_univariate_modular_system_of_linear_equation_v0_cc_code">35. <code>math/univariate_modular_system_of_linear_equation_v0.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// poj2115</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：一个数初始为A，不断在模2^k意义下加C，求第一次加到B的次数。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       如果不行输出"FOREVER"</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：设次数为x，则 (A + x*C) 同余于 B(mod 2^k),</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       即x*C 同余于 (B-A)(mod 2^k)</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run : $exec &lt; input &gt; output</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #009900">long</span> <span style="color: #009900">long</span> value_type<span style="color: #990000">;</span>

<span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">gcd</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(!</span>b<span style="color: #990000">)</span> <span style="color: #990000">?</span> a <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">gcd</span></span><span style="color: #990000">(</span>b<span style="color: #990000">,</span> a <span style="color: #990000">%</span> b<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> a<span style="color: #990000">,</span>  <span style="color: #008080">value_type</span> b<span style="color: #990000">,</span> value_type <span style="color: #990000">&amp;</span> d<span style="color: #990000">,</span> value_type <span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> value_type <span style="color: #990000">&amp;</span> y<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>b<span style="color: #990000">)</span> <span style="color: #FF0000">{</span> d <span style="color: #990000">=</span> a<span style="color: #990000">;</span> x <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> y <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span>b<span style="color: #990000">,</span> a <span style="color: #990000">%</span> b<span style="color: #990000">,</span> d<span style="color: #990000">,</span> y<span style="color: #990000">,</span> x<span style="color: #990000">);</span>
                y <span style="color: #990000">-=</span> x <span style="color: #990000">*</span> <span style="color: #990000">(</span>a<span style="color: #990000">/</span>b<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #008080">value_type</span> <span style="font-weight: bold"><span style="color: #000000">umsole</span></span><span style="color: #990000">(</span><span style="color: #008080">value_type</span> a<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> b<span style="color: #990000">,</span> <span style="color: #008080">value_type</span> p<span style="color: #990000">)</span>
        <span style="font-style: italic"><span style="color: #9A1900">// univariate_modular_system_of_linear_equation</span></span>
<span style="color: #FF0000">{</span>
        <span style="font-style: italic"><span style="color: #9A1900">// a*x  is congruent to  b  modulo  p</span></span>
        <span style="font-style: italic"><span style="color: #9A1900">// =&gt; a*x + p*(-y) = b</span></span>
        <span style="color: #008080">value_type</span> x<span style="color: #990000">,</span> y<span style="color: #990000">,</span> b0<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #000000">extended_gcd</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> p<span style="color: #990000">,</span> b0<span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">);</span>
        <span style="color: #008080">value_type</span> m <span style="color: #990000">=</span> p <span style="color: #990000">/</span> b0<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">((</span>x <span style="color: #990000">*</span> <span style="color: #990000">(</span>b <span style="color: #990000">/</span> b0<span style="color: #990000">))</span> <span style="color: #990000">%</span> m <span style="color: #990000">+</span> m<span style="color: #990000">)</span> <span style="color: #990000">%</span> m<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="color: #008080">value_type</span> a<span style="color: #990000">,</span> b<span style="color: #990000">,</span> c<span style="color: #990000">,</span> k<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> a <span style="color: #990000">&gt;&gt;</span> b <span style="color: #990000">&gt;&gt;</span> c <span style="color: #990000">&gt;&gt;</span> k <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">(</span>a <span style="color: #990000">||</span> b <span style="color: #990000">||</span> c <span style="color: #990000">||</span> k<span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                k <span style="color: #990000">=</span> 1ll <span style="color: #990000">&lt;&lt;</span> k<span style="color: #990000">;</span>
                b <span style="color: #990000">-=</span> a<span style="color: #990000">;</span>
                b <span style="color: #990000">=</span> <span style="color: #990000">((</span>b <span style="color: #990000">%</span> k<span style="color: #990000">)</span> <span style="color: #990000">+</span> k<span style="color: #990000">)</span> <span style="color: #990000">%</span> k<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>b <span style="color: #990000">%</span> <span style="font-weight: bold"><span style="color: #000000">gcd</span></span><span style="color: #990000">(</span>c<span style="color: #990000">,</span> k<span style="color: #990000">))</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"FOREVER</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="font-weight: bold"><span style="color: #000000">umsole</span></span><span style="color: #990000">(</span>c<span style="color: #990000">,</span> b<span style="color: #990000">,</span> k<span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_random_random_cc_code">36. <code>random/random.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;ctime&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdlib&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">srand</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">time</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">));</span>
        <span style="color: #009900">int</span> n <span style="color: #990000">=</span> <span style="color: #993399">10</span><span style="color: #990000">,</span> upper <span style="color: #990000">=</span> <span style="color: #993399">20</span><span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> n <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span> <span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="font-weight: bold"><span style="color: #000000">rand</span></span><span style="color: #990000">()</span> <span style="color: #990000">%</span> upper <span style="color: #990000">+</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_random_tree_cc_code">37. <code>random/tree.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;ctime&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdlib&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">200</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> map<span style="color: #990000">[</span>maxn<span style="color: #990000">][</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n <span style="color: #990000">=</span> <span style="color: #993399">20</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">srand</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">time</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">));</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> n <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
                std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> a<span style="color: #990000">;</span>
                std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> b<span style="color: #FF0000">{</span><span style="color: #993399">1</span><span style="color: #FF0000">}</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;=</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>i<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> x <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">rand</span></span><span style="color: #990000">()</span> <span style="color: #990000">%</span> a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
                        <span style="color: #009900">int</span> y <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">rand</span></span><span style="color: #990000">()</span> <span style="color: #990000">%</span> b<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
                        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> a<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span> <span style="color: #990000">&lt;&lt;</span> b<span style="color: #990000">[</span>y<span style="color: #990000">]</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
                        b<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>a<span style="color: #990000">[</span>x<span style="color: #990000">]);</span>
                        a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">erase</span></span><span style="color: #990000">(</span>a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">()</span> <span style="color: #990000">+</span> x<span style="color: #990000">);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_string_min_representation_cc_code">38. <code>string/min-representation.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// bzoj2176</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;string&gt;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">print</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="color: #008080">string</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> s<span style="color: #990000">,</span> <span style="color: #009900">int</span> p<span style="color: #990000">,</span> <span style="color: #009900">int</span> l<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> l<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> s<span style="color: #990000">[</span>p <span style="color: #990000">+</span> i<span style="color: #990000">];</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span><span style="color: #009900">char</span> a<span style="color: #990000">,</span> <span style="color: #009900">char</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span><span style="color: #009900">unsigned</span> <span style="color: #009900">char</span><span style="color: #990000">)</span>a <span style="color: #990000">&lt;</span> <span style="color: #990000">(</span><span style="color: #009900">unsigned</span> <span style="color: #009900">char</span><span style="color: #990000">)</span>b<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">minimum_representation</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="color: #008080">string</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> s<span style="color: #990000">,</span> <span style="color: #009900">int</span> l<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> j <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> t <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> l <span style="color: #990000">&amp;&amp;</span> j <span style="color: #990000">&lt;</span> l<span style="color: #990000">;</span> <span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>s<span style="color: #990000">[</span>i<span style="color: #990000">],</span> s<span style="color: #990000">[</span>j<span style="color: #990000">]))</span> j<span style="color: #990000">++;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>s<span style="color: #990000">[</span>j<span style="color: #990000">],</span> s<span style="color: #990000">[</span>i<span style="color: #990000">]))</span> i <span style="color: #990000">=</span> j<span style="color: #990000">,</span> j<span style="color: #990000">++;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>t <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> t <span style="color: #990000">&lt;</span> l<span style="color: #990000">;</span> t<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>s<span style="color: #990000">[</span>i <span style="color: #990000">+</span> t<span style="color: #990000">],</span> s<span style="color: #990000">[</span>j <span style="color: #990000">+</span> t<span style="color: #990000">]))</span> <span style="color: #FF0000">{</span> j <span style="color: #990000">+=</span> t<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>s<span style="color: #990000">[</span>j <span style="color: #990000">+</span> t<span style="color: #990000">],</span> s<span style="color: #990000">[</span>i <span style="color: #990000">+</span> t<span style="color: #990000">]))</span> <span style="color: #FF0000">{</span> i <span style="color: #990000">=</span> j<span style="color: #990000">;</span> j <span style="color: #990000">=</span> <span style="color: #990000">(</span>j <span style="color: #990000">+</span> t<span style="color: #990000">)</span> <span style="color: #990000">%</span> l<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span> <span style="font-style: italic"><span style="color: #9A1900">// this line is important</span></span>
                        <span style="color: #FF0000">}</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t <span style="color: #990000">==</span> l<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> i<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-style: italic"><span style="color: #9A1900">// std::ios::sync_with_stdio(false); FIXME add this line get runtime error</span></span>
        <span style="color: #009900">int</span> n<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">string</span> s<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> n<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">getline</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span>cin<span style="color: #990000">,</span> s<span style="color: #990000">);</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">getline</span></span><span style="color: #990000">(</span>std<span style="color: #990000">::</span>cin<span style="color: #990000">,</span> s<span style="color: #990000">);</span>
        s <span style="color: #990000">+=</span> s<span style="color: #990000">;</span>
        <span style="color: #009900">int</span> p <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">minimum_representation</span></span><span style="color: #990000">(</span>s<span style="color: #990000">,</span> n<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #000000">print</span></span><span style="color: #990000">(</span>s<span style="color: #990000">,</span> p<span style="color: #990000">,</span> n<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_string_suffix_array_v0_string_cc_code">39. <code>string/suffix_array_v0_string.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;string&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">suffix_array</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span><span style="color: #009900">int</span><span style="color: #990000">&gt;</span> <span style="color: #990000">&amp;</span> v_ref<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> v_cref<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">string</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> s_cref<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">suffix_cmp</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">suffix_cmp</span></span><span style="color: #990000">(</span><span style="color: #008080">v_cref</span> rank<span style="color: #990000">,</span> <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> gap<span style="color: #990000">,</span> <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> len<span style="color: #990000">)</span>
                        <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">rank</span></span><span style="color: #990000">(</span>rank<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">gap</span></span><span style="color: #990000">(</span>gap<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">len</span></span><span style="color: #990000">(</span>len<span style="color: #990000">)</span> <span style="color: #FF0000">{}</span>

                <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #0000FF">operator</span></span><span style="color: #990000">()(</span><span style="color: #009900">int</span> i<span style="color: #990000">,</span> <span style="color: #009900">int</span> j<span style="color: #990000">)</span>
                <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> rank<span style="color: #990000">[</span>j<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> rank<span style="color: #990000">[</span>j<span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>i <span style="color: #990000">+</span> gap <span style="color: #990000">&lt;</span> len <span style="color: #990000">&amp;&amp;</span> j <span style="color: #990000">+</span> gap <span style="color: #990000">&lt;</span> len<span style="color: #990000">)</span> <span style="color: #990000">?</span>
                                                <span style="color: #990000">(</span>rank<span style="color: #990000">[</span>i <span style="color: #990000">+</span> gap<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> rank<span style="color: #990000">[</span>j <span style="color: #990000">+</span> gap<span style="color: #990000">])</span> <span style="color: #990000">:</span> i <span style="color: #990000">&gt;</span> j<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>

        <span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
                <span style="color: #008080">v_cref</span> rank<span style="color: #990000">;</span>
                <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> gap<span style="color: #990000">;</span>
                <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> len<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #000000">suffix_array</span></span><span style="color: #990000">(</span><span style="color: #008080">s_cref</span> s<span style="color: #990000">,</span> <span style="color: #008080">v_ref</span> sa<span style="color: #990000">,</span> <span style="color: #008080">v_ref</span> rank<span style="color: #990000">,</span> <span style="color: #008080">v_ref</span> height<span style="color: #990000">)</span>
                <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">s</span></span><span style="color: #990000">(</span>s<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">sa</span></span><span style="color: #990000">(</span>sa<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">rank</span></span><span style="color: #990000">(</span>rank<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">height</span></span><span style="color: #990000">(</span>height<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                tmp_rank<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>rank<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">());</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">build_suffix_array</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> n <span style="color: #990000">=</span> s<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span> sa<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">;</span> rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> s<span style="color: #990000">[</span>i<span style="color: #990000">];</span> <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> gap <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> <span style="color: #990000">;</span> gap <span style="color: #990000">*=</span> <span style="color: #993399">2</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #008080">suffix_cmp</span> <span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>rank<span style="color: #990000">,</span> gap<span style="color: #990000">,</span> n<span style="color: #990000">);</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>sa<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> sa<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">()</span> <span style="color: #990000">+</span> n<span style="color: #990000">,</span> cmp<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                                tmp_rank<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> tmp_rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>sa<span style="color: #990000">[</span>i<span style="color: #990000">],</span> sa<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">]);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> rank<span style="color: #990000">[</span>sa<span style="color: #990000">[</span>i<span style="color: #990000">]]</span> <span style="color: #990000">=</span> tmp_rank<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tmp_rank<span style="color: #990000">[</span>n <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> n <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">build_lcp</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="color: #009900">int</span> n <span style="color: #990000">=</span> s<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> k <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> sa<span style="color: #990000">[</span>rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">];</span> s<span style="color: #990000">[</span>i <span style="color: #990000">+</span> k<span style="color: #990000">]</span> <span style="color: #990000">==</span> s<span style="color: #990000">[</span>j <span style="color: #990000">+</span> k<span style="color: #990000">];</span> k<span style="color: #990000">++);</span>
                                height<span style="color: #990000">[</span>rank<span style="color: #990000">[</span>i<span style="color: #990000">]]</span> <span style="color: #990000">=</span> k<span style="color: #990000">;</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>k<span style="color: #990000">)</span> k<span style="color: #990000">--;</span>
                        <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
        <span style="color: #008080">s_cref</span> s<span style="color: #990000">;</span>
        <span style="color: #008080">v_ref</span> sa<span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// suffix array</span></span>
        <span style="color: #008080">v_ref</span> rank<span style="color: #990000">;</span>
        <span style="color: #008080">v_ref</span> height<span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// lcp array, longest common prefix array</span></span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> tmp_rank<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="color: #008080">string</span> s<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cin <span style="color: #990000">&gt;&gt;</span> s<span style="color: #990000">;</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">sa</span></span><span style="color: #990000">(</span>s<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">());</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">rank</span></span><span style="color: #990000">(</span>s<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">());</span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">height</span></span><span style="color: #990000">(</span>s<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">());</span>
        <span style="color: #008080">suffix_array</span> <span style="font-weight: bold"><span style="color: #000000">suffix</span></span><span style="color: #990000">(</span>s<span style="color: #990000">,</span> sa<span style="color: #990000">,</span> rank<span style="color: #990000">,</span> height<span style="color: #990000">);</span>
        suffix<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">build_suffix_array</span></span><span style="color: #990000">();</span>
        suffix<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">build_lcp</span></span><span style="color: #990000">();</span>

        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> s <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">:</span> sa<span style="color: #990000">)</span>
                std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> s<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">substr</span></span><span style="color: #990000">(</span>i<span style="color: #990000">,</span> s<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">-</span> i<span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">'</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">'</span><span style="color: #990000">;</span>

        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"sa :"</span> <span style="color: #990000">&lt;&lt;</span> sa<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">:</span> sa<span style="color: #990000">)</span> std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> i <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span><span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"</span><span style="color: #CC33CC">\n\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>

        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"rank :"</span> <span style="color: #990000">&lt;&lt;</span> rank<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">:</span> rank<span style="color: #990000">)</span> std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> i <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span><span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"</span><span style="color: #CC33CC">\n\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>

        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"height :"</span> <span style="color: #990000">&lt;&lt;</span> height<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">auto</span></span> i <span style="color: #990000">:</span> height<span style="color: #990000">)</span> std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> i <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">' '</span><span style="color: #990000">;</span>
        std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"</span><span style="color: #CC33CC">\n\n</span><span style="color: #FF0000">"</span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_string_suffix_array_v0_vector_cc_code">40. <code>string/suffix_array_v0_vector.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">/* poj1743</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> *</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * 大意是找不可重叠最长重复子串，不过要对题意进行一些转换。</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> *</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * 这里将suffix_array用vector封装，使得串里的数可以是负数。</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * 对于这个版本代码还要进行理解，主要是关于末尾需不需再要加最小数的问题。</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> *</span></span>
<span style="font-style: italic"><span style="color: #9A1900">*/</span></span>

<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;algorithm&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">suffix_array</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span><span style="color: #009900">int</span><span style="color: #990000">&gt;</span> <span style="color: #990000">&amp;</span> v_ref<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> v_cref<span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">suffix_cmp</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #000000">suffix_cmp</span></span><span style="color: #990000">(</span><span style="color: #008080">v_cref</span> rank<span style="color: #990000">,</span> <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> gap<span style="color: #990000">,</span> <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> len<span style="color: #990000">)</span>
                        <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">rank</span></span><span style="color: #990000">(</span>rank<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">gap</span></span><span style="color: #990000">(</span>gap<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">len</span></span><span style="color: #990000">(</span>len<span style="color: #990000">)</span> <span style="color: #FF0000">{}</span>

                <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #0000FF">operator</span></span><span style="color: #990000">()(</span><span style="color: #009900">int</span> i<span style="color: #990000">,</span> <span style="color: #009900">int</span> j<span style="color: #990000">)</span>
                <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> rank<span style="color: #990000">[</span>j<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> rank<span style="color: #990000">[</span>j<span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">(</span>i <span style="color: #990000">+</span> gap <span style="color: #990000">&lt;</span> len <span style="color: #990000">&amp;&amp;</span> j <span style="color: #990000">+</span> gap <span style="color: #990000">&lt;</span> len<span style="color: #990000">)</span> <span style="color: #990000">?</span>
                                                <span style="color: #990000">(</span>rank<span style="color: #990000">[</span>i <span style="color: #990000">+</span> gap<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> rank<span style="color: #990000">[</span>j <span style="color: #990000">+</span> gap<span style="color: #990000">])</span> <span style="color: #990000">:</span> i <span style="color: #990000">&gt;</span> j<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>

        <span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
                <span style="color: #008080">v_cref</span> rank<span style="color: #990000">;</span>
                <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> gap<span style="color: #990000">;</span>
                <span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #990000">&amp;</span> len<span style="color: #990000">;</span>
        <span style="color: #FF0000">}</span><span style="color: #990000">;</span>

        <span style="font-weight: bold"><span style="color: #000000">suffix_array</span></span><span style="color: #990000">(</span><span style="color: #008080">v_cref</span> s<span style="color: #990000">,</span> <span style="color: #009900">int</span> n<span style="color: #990000">,</span> <span style="color: #008080">v_ref</span> sa<span style="color: #990000">,</span> <span style="color: #008080">v_ref</span> rank<span style="color: #990000">,</span> <span style="color: #008080">v_ref</span> height<span style="color: #990000">)</span>
                <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">s</span></span><span style="color: #990000">(</span>s<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">n</span></span><span style="color: #990000">(</span>n<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">sa</span></span><span style="color: #990000">(</span>sa<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">rank</span></span><span style="color: #990000">(</span>rank<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">height</span></span><span style="color: #990000">(</span>height<span style="color: #990000">)</span>
        <span style="color: #FF0000">{</span>
                tmp_rank<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">resize</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">build_suffix_array</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span> sa<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> i<span style="color: #990000">;</span> rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> s<span style="color: #990000">[</span>i<span style="color: #990000">];</span> <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> gap <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> <span style="color: #990000">;</span> gap <span style="color: #990000">*=</span> <span style="color: #993399">2</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #008080">suffix_cmp</span> <span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>rank<span style="color: #990000">,</span> gap<span style="color: #990000">,</span> n<span style="color: #990000">);</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">sort</span></span><span style="color: #990000">(</span>sa<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">(),</span> sa<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">()</span> <span style="color: #990000">+</span> n<span style="color: #990000">,</span> cmp<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                                tmp_rank<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> tmp_rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #000000">cmp</span></span><span style="color: #990000">(</span>sa<span style="color: #990000">[</span>i<span style="color: #990000">],</span> sa<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">]);</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> rank<span style="color: #990000">[</span>sa<span style="color: #990000">[</span>i<span style="color: #990000">]]</span> <span style="color: #990000">=</span> tmp_rank<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>tmp_rank<span style="color: #990000">[</span>n <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> n <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">break</span></span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

        <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">build_lcp</span></span><span style="color: #990000">()</span>
        <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> k <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">!=</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> j <span style="color: #990000">=</span> sa<span style="color: #990000">[</span>rank<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">];</span> s<span style="color: #990000">[</span>i <span style="color: #990000">+</span> k<span style="color: #990000">]</span> <span style="color: #990000">==</span> s<span style="color: #990000">[</span>j <span style="color: #990000">+</span> k<span style="color: #990000">];</span> k<span style="color: #990000">++);</span>
                                height<span style="color: #990000">[</span>rank<span style="color: #990000">[</span>i<span style="color: #990000">]]</span> <span style="color: #990000">=</span> k<span style="color: #990000">;</span>
                                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>k<span style="color: #990000">)</span> k<span style="color: #990000">--;</span>
                        <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
        <span style="color: #008080">v_cref</span> s<span style="color: #990000">;</span>
        <span style="color: #009900">int</span> n<span style="color: #990000">;</span>
        <span style="color: #008080">v_ref</span> sa<span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// suffix array</span></span>
        <span style="color: #008080">v_ref</span> rank<span style="color: #990000">;</span>
        <span style="color: #008080">v_ref</span> height<span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// lcp array, longest common prefix array</span></span>
        std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> tmp_rank<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">20017</span><span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">sa</span></span><span style="color: #990000">(</span>maxn<span style="color: #990000">);</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">rank</span></span><span style="color: #990000">(</span>maxn<span style="color: #990000">);</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">height</span></span><span style="color: #990000">(</span>maxn<span style="color: #990000">);</span>
std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;int&gt;</span> <span style="font-weight: bold"><span style="color: #000000">da</span></span><span style="color: #990000">(</span>maxn<span style="color: #990000">);</span>
<span style="color: #009900">int</span> n<span style="color: #990000">;</span>

<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">judge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> len<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> min <span style="color: #990000">=</span> sa<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> max <span style="color: #990000">=</span> sa<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>height<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">&lt;</span> len<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>max <span style="color: #990000">-</span> min <span style="color: #990000">&gt;</span> len<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
                        max <span style="color: #990000">=</span> sa<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">];</span> min <span style="color: #990000">=</span> sa<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">];</span>
                <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        max <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">max</span></span><span style="color: #990000">(</span>max<span style="color: #990000">,</span> sa<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">]);</span>
                        min <span style="color: #990000">=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">min</span></span><span style="color: #990000">(</span>min<span style="color: #990000">,</span> sa<span style="color: #990000">[</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">]);</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> max <span style="color: #990000">-</span> min <span style="color: #990000">&gt;</span> len<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> first<span style="color: #990000">;</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">)</span> <span style="color: #990000">&amp;&amp;</span> n<span style="color: #990000">;</span> <span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>first<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>n <span style="color: #990000">==</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"0</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">);</span> <span style="font-weight: bold"><span style="color: #0000FF">continue</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> tmp<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>tmp<span style="color: #990000">);</span>
                        da<span style="color: #990000">[</span>i<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> tmp <span style="color: #990000">-</span> first<span style="color: #990000">;</span>
                        first <span style="color: #990000">=</span> tmp<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>

                <span style="color: #008080">suffix_array</span> <span style="font-weight: bold"><span style="color: #000000">suffix</span></span><span style="color: #990000">(</span>da<span style="color: #990000">,</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">,</span> sa<span style="color: #990000">,</span> rank<span style="color: #990000">,</span> height<span style="color: #990000">);</span>
                suffix<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">build_suffix_array</span></span><span style="color: #990000">();</span>
                suffix<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">build_lcp</span></span><span style="color: #990000">();</span>

                <span style="color: #009900">int</span> l <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> r <span style="color: #990000">=</span> n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">while</span></span> <span style="color: #990000">(</span>l <span style="color: #990000">+</span> <span style="color: #993399">1</span> <span style="color: #990000">&lt;</span> r<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
                        <span style="color: #009900">int</span> mid <span style="color: #990000">=</span> <span style="color: #990000">(</span>l <span style="color: #990000">+</span> r<span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">judge</span></span><span style="color: #990000">(</span>mid<span style="color: #990000">))</span> l <span style="color: #990000">=</span> mid<span style="color: #990000">;</span>
                        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>                    r <span style="color: #990000">=</span> mid <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">judge</span></span><span style="color: #990000">(</span>r<span style="color: #990000">))</span> l <span style="color: #990000">=</span> r<span style="color: #990000">;</span>
                <span style="color: #009900">int</span> ans <span style="color: #990000">=</span> l <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>

                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>ans <span style="color: #990000">&lt;</span> <span style="color: #993399">5</span><span style="color: #990000">)</span> ans <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> ans<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_code_string_suffix_automaton_cc_code">41. <code>string/suffix_automaton.cc</code></h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">// bzoj3926</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题意：给定一个叶子树不超过20的树，树上每个节点有颜色，颜色类型不超过10,</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       树上两个节点确定一条路径，得到颜色序列，正反算两总，求有多少不同</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       颜色序列。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// 题解：反过来从每个叶子结点dfs建立一个大trie，然后由这个大trie建立广义</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       后缀自动机（多个串的自动机），然后就是在自动机上求不同子串个数，</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//       这是经典应用，用dp球从初始节点出发到所有节点不同路径数就行。</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// run: $exec &lt; input</span></span>
<span style="font-style: italic"><span style="color: #9A1900">// opt: 0</span></span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cstdio&gt;</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> alpha_size <span style="color: #990000">=</span> <span style="color: #993399">10</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> maxn <span style="color: #990000">=</span> <span style="color: #993399">100007</span><span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">state</span>
<span style="color: #FF0000">{</span>
        <span style="color: #009900">int</span> len<span style="color: #990000">;</span>
        state <span style="color: #990000">*</span> link<span style="color: #990000">,</span> <span style="color: #990000">*</span> child<span style="color: #990000">[</span>alpha_size<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span> sam_state<span style="color: #990000">[</span>maxn <span style="color: #990000">*</span> <span style="color: #993399">44</span><span style="color: #990000">],</span> <span style="color: #990000">*</span> sam_head<span style="color: #990000">;</span>

<span style="color: #009900">int</span> sam_alloc <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #008080">trie</span>
<span style="color: #FF0000">{</span>
        trie <span style="color: #990000">*</span> child<span style="color: #990000">[</span>alpha_size<span style="color: #990000">];</span>
<span style="color: #FF0000">}</span> trie_node<span style="color: #990000">[</span>maxn <span style="color: #990000">*</span> <span style="color: #993399">22</span><span style="color: #990000">],</span> <span style="color: #990000">*</span> trie_head<span style="color: #990000">;</span>

<span style="color: #009900">int</span> trie_alloc <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>

<span style="color: #009900">int</span> next<span style="color: #990000">[</span><span style="color: #993399">2</span> <span style="color: #990000">*</span> maxn<span style="color: #990000">],</span> end_point<span style="color: #990000">[</span><span style="color: #993399">2</span> <span style="color: #990000">*</span> maxn<span style="color: #990000">],</span> head<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> alloc <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
<span style="color: #009900">int</span> color<span style="color: #990000">[</span>maxn<span style="color: #990000">],</span> degree<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">bool</span> vis<span style="color: #990000">[</span>maxn<span style="color: #990000">];</span>
<span style="color: #009900">int</span> n<span style="color: #990000">,</span> m<span style="color: #990000">;</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> u<span style="color: #990000">,</span> <span style="color: #009900">int</span> v<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        end_point<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> v<span style="color: #990000">;</span> next<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> head<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> alloc<span style="color: #990000">++;</span>
        end_point<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> u<span style="color: #990000">;</span> next<span style="color: #990000">[</span>alloc<span style="color: #990000">]</span> <span style="color: #990000">=</span> head<span style="color: #990000">[</span>v<span style="color: #990000">];</span> head<span style="color: #990000">[</span>v<span style="color: #990000">]</span> <span style="color: #990000">=</span> alloc<span style="color: #990000">++;</span>
        degree<span style="color: #990000">[</span>u<span style="color: #990000">]++;</span>  degree<span style="color: #990000">[</span>v<span style="color: #990000">]++;</span>
<span style="color: #FF0000">}</span>

trie <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">trie_node_alloc</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        trie <span style="color: #990000">*</span> tmp <span style="color: #990000">=</span> trie_node <span style="color: #990000">+</span> trie_alloc<span style="color: #990000">++;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> tmp<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">build_trie</span></span><span style="color: #990000">(</span>trie <span style="color: #990000">*</span> now<span style="color: #990000">,</span> <span style="color: #009900">int</span> u<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>now<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>color<span style="color: #990000">[</span>u<span style="color: #990000">]])</span>
                now<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>color<span style="color: #990000">[</span>u<span style="color: #990000">]]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">trie_node_alloc</span></span><span style="color: #990000">();</span>
        now <span style="color: #990000">=</span> now<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>color<span style="color: #990000">[</span>u<span style="color: #990000">]];</span>
        vis<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> p <span style="color: #990000">=</span> head<span style="color: #990000">[</span>u<span style="color: #990000">];</span> p<span style="color: #990000">;</span> p <span style="color: #990000">=</span> next<span style="color: #990000">[</span>p<span style="color: #990000">])</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>vis<span style="color: #990000">[</span>end_point<span style="color: #990000">[</span>p<span style="color: #990000">]])</span>
                        <span style="font-weight: bold"><span style="color: #000000">build_trie</span></span><span style="color: #990000">(</span>now<span style="color: #990000">,</span> end_point<span style="color: #990000">[</span>p<span style="color: #990000">]);</span>
        vis<span style="color: #990000">[</span>u<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

state <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">sam_state_alloc</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        state <span style="color: #990000">*</span> tmp <span style="color: #990000">=</span> sam_state <span style="color: #990000">+</span> sam_alloc<span style="color: #990000">++;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> tmp<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

state <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">sam_extend</span></span><span style="color: #990000">(</span>state <span style="color: #990000">*</span> last<span style="color: #990000">,</span> <span style="color: #009900">int</span> x<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900">// x [0, alpha_sie) here is m</span></span>
<span style="color: #FF0000">{</span>
        state <span style="color: #990000">*</span> cur <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">sam_state_alloc</span></span><span style="color: #990000">();</span>
        cur<span style="color: #990000">-&gt;</span>len <span style="color: #990000">=</span> last<span style="color: #990000">-&gt;</span>len <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        state <span style="color: #990000">*</span> p<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>p <span style="color: #990000">=</span> last<span style="color: #990000">;</span> p <span style="color: #990000">&amp;&amp;</span> <span style="color: #990000">!</span>p<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>x<span style="color: #990000">];</span> p <span style="color: #990000">=</span> p<span style="color: #990000">-&gt;</span>link<span style="color: #990000">)</span>
                p<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> cur<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(!</span>p<span style="color: #990000">)</span> cur<span style="color: #990000">-&gt;</span>link <span style="color: #990000">=</span> sam_head<span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                state <span style="color: #990000">*</span> q <span style="color: #990000">=</span> p<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>x<span style="color: #990000">];</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>p<span style="color: #990000">-&gt;</span>len <span style="color: #990000">+</span> <span style="color: #993399">1</span> <span style="color: #990000">==</span> q<span style="color: #990000">-&gt;</span>len<span style="color: #990000">)</span>
                        cur<span style="color: #990000">-&gt;</span>link <span style="color: #990000">=</span> q<span style="color: #990000">;</span>
                <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="color: #FF0000">{</span>
                        state <span style="color: #990000">*</span> clone <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">sam_state_alloc</span></span><span style="color: #990000">();</span>
                        <span style="font-style: italic"><span style="color: #9A1900">// clone</span></span>
                        <span style="color: #990000">*</span>clone <span style="color: #990000">=</span> <span style="color: #990000">*</span>q<span style="color: #990000">;</span>
                        clone<span style="color: #990000">-&gt;</span>len <span style="color: #990000">=</span> p<span style="color: #990000">-&gt;</span>len <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>

                        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(;</span> p <span style="color: #990000">&amp;&amp;</span> p<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">==</span> q<span style="color: #990000">;</span> p <span style="color: #990000">=</span> p<span style="color: #990000">-&gt;</span>link<span style="color: #990000">)</span>
                                p<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>x<span style="color: #990000">]</span> <span style="color: #990000">=</span> clone<span style="color: #990000">;</span>
                        q<span style="color: #990000">-&gt;</span>link <span style="color: #990000">=</span> cur<span style="color: #990000">-&gt;</span>link <span style="color: #990000">=</span> clone<span style="color: #990000">;</span>
                <span style="color: #FF0000">}</span>
        <span style="color: #FF0000">}</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> cur<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">build_sam</span></span><span style="color: #990000">(</span>state <span style="color: #990000">*</span> now<span style="color: #990000">,</span> trie <span style="color: #990000">*</span> t<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>i<span style="color: #990000">])</span> <span style="color: #FF0000">{</span>
                        state <span style="color: #990000">*</span> tmp <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">sam_extend</span></span><span style="color: #990000">(</span>now<span style="color: #990000">,</span> i<span style="color: #990000">);</span>
                        <span style="font-weight: bold"><span style="color: #000000">build_sam</span></span><span style="color: #990000">(</span>tmp<span style="color: #990000">,</span> t<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
                <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">long</span> <span style="color: #009900">long</span> path_count<span style="color: #990000">[</span><span style="color: #993399">44</span> <span style="color: #990000">*</span> maxn<span style="color: #990000">];</span>

<span style="color: #009900">long</span> <span style="color: #009900">long</span> <span style="font-weight: bold"><span style="color: #000000">number_of_different_substring</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> s<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
        <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>path_count<span style="color: #990000">[</span>s<span style="color: #990000">])</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> path_count<span style="color: #990000">[</span>s<span style="color: #990000">];</span>
        state <span style="color: #990000">*</span> ts <span style="color: #990000">=</span> sam_state <span style="color: #990000">+</span> s<span style="color: #990000">;</span>
        <span style="color: #009900">long</span> <span style="color: #009900">long</span> count <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>ts<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>i<span style="color: #990000">])</span>
                        count <span style="color: #990000">+=</span> <span style="font-weight: bold"><span style="color: #000000">number_of_different_substring</span></span><span style="color: #990000">(</span>ts<span style="color: #990000">-&gt;</span>child<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">-</span> sam_state<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> path_count<span style="color: #990000">[</span>s<span style="color: #990000">]</span> <span style="color: #990000">=</span> count<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>n<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>m<span style="color: #990000">);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>color<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">,</span> x<span style="color: #990000">,</span> y<span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span> <span style="color: #FF0000">{</span>
                std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">scanf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%d %d"</span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>y<span style="color: #990000">);</span>
                <span style="font-weight: bold"><span style="color: #000000">add_edge</span></span><span style="color: #990000">(--</span>x<span style="color: #990000">,</span> <span style="color: #990000">--</span>y<span style="color: #990000">);</span>
        <span style="color: #FF0000">}</span>

        trie_head <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">trie_node_alloc</span></span><span style="color: #990000">();</span>
        <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span><span style="color: #009900">int</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
                <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>degree<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #000000">build_trie</span></span><span style="color: #990000">(</span>trie_head<span style="color: #990000">,</span> i<span style="color: #990000">);</span>

        sam_head <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">sam_state_alloc</span></span><span style="color: #990000">();</span>
        <span style="font-weight: bold"><span style="color: #000000">build_sam</span></span><span style="color: #990000">(</span>sam_head<span style="color: #990000">,</span> trie_head<span style="color: #990000">);</span>
        std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">printf</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"%lld</span><span style="color: #CC33CC">\n</span><span style="color: #FF0000">"</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #000000">number_of_different_substring</span></span><span style="color: #990000">(</span>sam_head <span style="color: #990000">-</span> sam_state<span style="color: #990000">)</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
</div>
<div id="footnotes"><hr /></div>
<div id="footer">
<div id="footer-text">
Last updated 2015-11-19 19:38:30 CST
</div>
</div>
</body>
</html>
